應用多型來做傳值的基本功大概就是上一篇所談的內容,這篇的內容著重的就是衍生的用法,如果你已經瞭解那個基本原理,大概就不需要再看這一篇。
應用多型來做傳值的基本功大概就是上一篇所談的內容,這篇的內容著重的就是衍生的用法,如果你已經瞭解那個基本原理,大概就不需要再看這一篇。
這一篇的例子我把它倒過來做,當一個 MainForm 開啟好幾個 ChildForm ,同時傳遞某些值過去,產生以下範例畫面的效果。
ChildForm1 與 ChildForm2 沒啥大不了,就是個Label而已,不過ChildForm3上的 『分舵主』卻是用GDI+去畫的。
因為DataGridView的資料來源的關係,所以先寫一個類別來描述一份資料,為了避免模糊焦點,此 SampleData Class 寫的很單純:
Public Class SampleData
Public Property Name As String
Public Property Phone As String
Public Property Title As String
Sub New(ByVal userName As String, ByVal userPhone As String, ByVal userTitle As String)
Name = userName
Phone = userPhone
Title = userTitle
End Sub
End Class
聰明的你一定知道下一步又是要建 Interface 了,由於和上一篇傳值的方向相反,所以這個 Interface 也一樣反過來是給被呼叫者實做的。
Public Interface ISlave
WriteOnly Property Passvalue As String
End Interface
所以我們先來在 ChildForm1 與 ChildForm2 上實做 ISlave
Public Class ChildForm1
Implements ISlave
Public WriteOnly Property Passvalue As String Implements ISlave.Passvalue
Set(ByVal value As String)
Label1.Text = value
End Set
End Property
End Class
Public Class ChildForm2
Implements ISlave
Public WriteOnly Property Passvalue As String Implements ISlave.Passvalue
Set(ByVal value As String)
Label1.Text = value
End Set
End Property
End Class
因為 ChildForm3 是用GDI+來畫,所以基本上內情不單純,沒法直接在屬性中Set就結束,它必須要用到Paint事件來做
Public Class ChildForm3
Implements ISlave
Private panelString As String
Public WriteOnly Property Passvalue As String Implements ISlave.Passvalue
Set(ByVal value As String)
panelString = value
End Set
End Property
Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
e.Graphics.DrawString(panelString, New Font("impact", 18), Brushes.Brown, New PointF(10, 10))
End Sub
End Class
那在 MainForm 要怎麼處理呢?因為我很懶所以全寫在一個Button Click來一次開三個Child Form,分別指派不同的值給不同的 ChildForm
Public Class MainForm
Private internalBindingsource As New BindingSource
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BuildTestData()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim popForm1 As New ChildForm1
popForm1.Passvalue = CType(internalBindingsource.Current, SampleData).Name
popForm1.Show()
Dim popForm2 As New ChildForm2
popForm2.Passvalue = CType(internalBindingsource.Current, SampleData).Phone
popForm2.Show()
Dim popForm3 As New ChildForm3
popForm3.Passvalue = CType(internalBindingsource.Current, SampleData).Title
popForm3.Show()
End Sub
''' <summary>
''' 建立並指派資料給DataGridView
''' </summary>
''' <remarks></remarks>
Private Sub BuildTestData()
Dim dataList As New List(Of SampleData)
dataList.Add(New SampleData("大毛", "0990123456", "扛霸子"))
dataList.Add(New SampleData("二毛", "0990111111", "分舵主"))
dataList.Add(New SampleData("三毛", "0990222222", "倒酒小弟"))
dataList.Add(New SampleData("小毛", "0990333333", "打雜"))
internalBindingsource.DataSource = dataList
DataGridView1.DataSource = internalBindingsource
End Sub
End Class
就這麼簡單,沒什麼太大的學問;如果你對於這種介面使用不熟悉,建議你可以想個更進一步的範例來練習,慢慢你會發現這玩意兒真是妙用無窮。