VB 課程進階第三天預排內容

1,136 views
1,031 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,136
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

VB 課程進階第三天預排內容

  1. 1. 巨匠電腦 葉建榮 Visual Basic 進階第三天1、 延續上一個練習,同一個專案內。建立物件及瞭解權限控管:1. 於 demo class 之後建立新的類別Public Class box Public a, b, c As Integer a b c 代表長寬高End Class2. 第 3 個按鈕 Private Sub Button3_Click(…) Handles Button3.Click Dim box1 As New box box1.a = TextBox1.Text box1.b = TextBox2.Text box1.c = TextBox3.Text Label1.Text = box1.a * box1.b * box1.c End Sub若輸入負數呢?權限控管為何重要?因為類別與程式流程可以拆成兩個專案進行。而程式不見得了解類別內的規範,人操作時難免會有狀況發生。以下將分兩個步驟進行:1. 請另開新的專案,新增專案,選類別庫2. 拷貝 Class box 語法至這個類別庫3. 類別庫專案請儲存後按下[偵錯]功能表內[建置....]4. 請另開新的專案,建立新的 windows 專案並且儲存5. 拷貝類別庫專案的 bin 之 Release 目錄內的 dll 檔至 windows 專案目錄 bin 之 debug 目錄內6. 於方案總管案右鍵選[加入參考],選[瀏覽],點選剛 copy 的 dll 檔7. 拷貝 Button3_Click 內與法,並請規劃放置相關元件8. 程式最前面加入 Imports 你的 Class 專案名稱2、 新增類別與表單按鈕,了解權限控管 此按鈕不能執行1. 新增類別Public Class box1 Dim a As Integer Protected b As Integer Private c As IntegerEnd Class2. 第 4 個按鈕:此範例將產生錯誤,無法執行 Private Sub Button4_Click(…) Handles Button4.Click Dim box1 As New box1 box1.a = TextBox1.Text box1.b = TextBox2.TextVB.Net 進階第三天上課 第1頁
  2. 2. 巨匠電腦 葉建榮 box1.c = TextBox3.Text Label1.Text = box1.a * box1.b * box1.c End Sub3、 前一個範例修改,增加成員函數1. 新增類別【function( )後加上 as integer 的影響】Public Class box2 Dim a As Integer Protected b As Integer Private c As Integer Public Function vol(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer) AsInteger If (a <= 0) Or (b <= 0) Or (c <= 0) Then MessageBox.Show("資料錯誤~") Return 0 Exit Function Else Return (a * b * c) * 0.5 End If End FunctionEnd Class2. 第 5 個按鈕:增加例外處理分析 Private Sub Button5_Click(…..) Handles Button5.Click Dim box1 As New box2 Try Label1.Text = box1.vol(TextBox1.Text, TextBox2.Text, TextBox3.Text) Catch ex As Exception MsgBox("請輸入數字") End Try End Sub4、 成績專案的練習(請參考網路硬碟上提供的專案)1)gradeclass 專案請[建置]=>若成績計算公式修改請重新建置2)請將 gradeclass 專案的[/bin/release]內的 dll 檔 copy 到 grade 的[binDebug]內3)執行 grade 專案,若不能執行,請重新執行[加入參考]類別以 dll 規劃的好處:1)當物件設定更改時,不用更改程式 2)dll 檔目錄必須與執行檔目錄相同5、 成績專案練習的延伸※自訂類別庫,若想執行其他函數,例如 MessageBox.Show,該如何執行呢?1)可先在 Msdn 網站上,查這個函數屬於哪一個 Class=>例如 MessageBox 屬於 System.Windows.FormVB.Net 進階第三天上課 第2頁
  3. 3. 巨匠電腦 葉建榮2)加入參考及 Imports6、 能力的繼承1. 製作一個繼承 textbox 的類別Class 部分Public Class EditField Inherits TextBoxEnd ClassWindows Form 部分Private Sub Button1_Click(….) Handles Button1.Click Dim MyEditField As EditField = New EditField Me.Controls.Add(MyEditField) MyEditField.Left = 10 MyEditField.Top = 10 MyEditField.Width = 75 MyEditField.Height = 15 MyEditField.Text = "MyEditField"End Sub2. 上一個專案修改,建立獨立的類別庫,可將 dll 加入工具箱3. 上一個專案修改,可針對新的工具於程式流程內加上事件及功能 Private Sub Button1_Click(…) Handles Button1.Click Dim MyEditField As editfield = New editfield Me.Controls.Add(MyEditField) MyEditField.Left = 10 MyEditField.Top = 10 MyEditField.Width = 75 MyEditField.Height = 15 MyEditField.Text = "MyEditField" AddHandler MyEditField.TextChanged, AddressOf test End Sub Private Sub test(ByVal sender As System.Object, ByVal e As System.EventArgs) MsgBox("a") End Sub4. 製作一個繼承 textbox 的類別,也加入新的功能Class 部分Public Class EditField Inherits TextBox Public Sub ReverseText( ) MyClass.Text = StrReverse(Me.Text) End SubVB.Net 進階第三天上課 第3頁
  4. 4. 巨匠電腦 葉建榮End ClassWindows Form 部分Private Sub Button1_Click(…) Handles Button1.Click TextBox1.Text = StrReverse(TextBox1.Text)End SubPrivate Sub Button2_Click(….) Handles Button2.Click Dim MyEditField As EditField= New EditField Me.Controls.Add(MyEditField) MyEditField.Text = "MyEditField" MyEditField.ReverseText( )End Sub5. 新的類別可以改寫原來類別的功能Class 部分Public Class EditField Inherits TextBox Public Overrides Sub ResetText( ) Dim msgboxstring As string msgboxstring= MsgBox("要清除?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo) If msgboxstring = MsgBoxResult.Yes Then MyClass.Text = "" End If End SubEnd ClassWindows Form 部分Private Sub Button1_Click(….) Handles Button1.Click Dim MyEditField As EditField = New EditField Me.Controls.Add(MyEditField) MyEditField.Text = "MyEditField" MyEditField.ResetText( )End SubPrivate Sub Button2_Click(…….) Handles Button2.Click TextBox1.ResetText( )End SubPrivate Sub Button3_Click(………) Handles Button3.Click Editfield1.ResetText( )End Sub7、 建立多個 Form,討論表單開啟與關閉方式1. Form1 部分 Public a As Integer = 0VB.Net 進階第三天上課 第4頁
  5. 5. 巨匠電腦 葉建榮 Private Sub Form1_Load(......) Handles MyBase.Load ListBox1.Items.Add("Form2") ListBox1.Items.Add("Form3") ListBox1.Items.Add("Form4") ListBox1.Items.Add("Form5") ListBox1.Items.Add("Form6") ListBox1.Items.Add("Form7") ListBox1.Items.Add("Form8") ListBox1.Items.Add("Form9") Label1.Text = Now( ).ToString Label2.Text = a End Sub Private Sub Button1_Click(......) Handles Button1.Click Select Case ListBox1.SelectedIndex Case 0 Dim f1 As Form = New Form2( ) f1.Show( ) Case 1 Dim f2 As Form = New Form3( ) f2.ShowDialog( ) Me.Hide( ) Case 2 Dim f3 As Form = New Form4( ) f3.Show(Me) Case 3 Me.Hide( ) Dim f4 As Form = New Form5( ) f4.Show(Me) Case 4 Me.Hide( ) Dim f5 As Form = New Form6( ) f5.Show( ) Case 5 Dim f6 As Form = New Form7( ) f6.ShowDialog(Me) Me.Hide( )VB.Net 進階第三天上課 第5頁
  6. 6. 巨匠電腦 葉建榮 Case 6 Dim f7 As Form = New Form8( ) f7.Show(Me) Case 7 Dim f8 As Form = New Form9( ) f8.Show( ) End Select End Sub Private Sub Button2_Click(......) Handles Button2.Click Label2.Text = a End Sub2. 各種開啟表單方式以 Show( )方式開啟 Form2以 ShowDialog( )方式開啟 Form3,Me.hide以 Show(Me )方式開啟 Form4以 Show(Me )方式開啟 Form5,Me.hide以 Show( )方式開啟 Form6,Me.hide以 ShowDialog(Me )方式開啟 Form7,Me.hide以 Show(Me )方式開啟 Form8以 Show( )方式開啟 Form93. Form1 請多開啟 Form2 及 Form4 比較 Form1 與 Form2 還有 Form1 及 Form4 可否互換上下層關係?4. Form3 與 Form6 差異5. Form6 若關閉 可呼叫 Form1 嗎?Public Class Form6 Private Sub Form6_FormClosed(......) Handles MyBase.FormClosed 語法可i能有問題 Owner.Visible = True Dim f3 As Form = New Form1( ) f3.Show( ) End Sub Private Sub Button1_Click(......) Handles Button1.Click Dim f3 As Form = New Form4( ) f3.Show( ) Me.Close( ) End SubVB.Net 進階第三天上課 第6頁
  7. 7. 巨匠電腦 葉建榮End Class6. Form5 部分Private Sub Button1_Click (.....) Handles Button1.Click Dim f3 As Form = New Form4( ) f3.Show( ) Me.Close( ) End SubPrivate Sub Form5_FormClosed(.....) Handles Form5.FormClosed Owner.Visible = TrueEnd Sub7. Form8 傳回資料 Private Sub Button1_Click(......) Handles Button1.Click CType(Owner, Form1).a = TextBox1.Text Owner.Visible = True End Sub8. Form9 傳回資料 Private Sub Button1_Click(......) Handles Button1.Click Dim f4 As Form = New Form1( ) CType(f4, Form1).a = TextBox1.Text f4.Show( ) End Sub8、 表單 1 與表單 2 分別傳值1. 建立表單 1:三個 textbox、第一個 buuton 作用為「open form2」 第二個 button 作用為 、 「get form2 data」, 第三個 button 是要進行測試Public form2 As Form2Private Sub Button1_Click(.....) Handles Button1.Click form2 = New Form2( ) form2.Owner = Me form2.Show( )End SubPublic Sub SetData(v1 As String, v2 As String) Me.textBox1.Text = v1 Me.textBox2.Text = v2End SubPrivate Sub Button2_Click(.....) Handles Button2.Click If form2 Is Nothing Then MessageBox.Show("請先開啟 form2") End IfVB.Net 進階第三天上課 第7頁
  8. 8. 巨匠電腦 葉建榮 Dim values As String( ) = form2.GetData( ) Me.textBox1.Text = values(0) Me.textBox2.Text = values(1)End SubPrivate Sub Button3_Click(.....) Handles Button3.Click form2 = New Form2( ) form2.Show( )End Sub2. 建立表單 2:兩個 textbox 與兩個 buttonPrivate Sub Button1_Click(.....) Handles Button1.Click CType (Me.Owner, Form1).SetData(Me.textBox1.Text, Me.textBox2.Text)End SubPublic Function GetData( ) As String( ) Return New String( ) {Me.textBox1.Text, Me.textBox2.Text}End FunctionPrivate Sub Button2_Click(.....) Handles Button2.Click Dim form1 As New Form1( ) form1.SetData(Me.textBox1.Text, Me.textBox2.Text)End Sub9、 其他VB.Net 進階第三天上課 第8頁

×