可以實(shí)現(xiàn)
創(chuàng)新互聯(lián)主營潮州網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶App定制開發(fā),潮州h5成都小程序開發(fā)搭建,潮州網(wǎng)站營銷推廣歡迎潮州等地區(qū)企業(yè)咨詢
首先創(chuàng)建一個(gè)Button類型控件數(shù)組:
1、創(chuàng)建“Windows應(yīng)用程序”類型的工程,添加名為ButtonArray的類,并使該類繼承 System.Collection.CollectionBase 類。System.Collections.CollectionBase類是.NET框架類庫中為集合操作提供抽象的基類,通過對它的繼承可以為我們的ButtonArray類具備集合增加、刪除、索引的功能。
2、為ButtonArray類添加ParentForm屬性,即控件組所在窗體,創(chuàng)建初始化函數(shù)(構(gòu)造函數(shù));
3、為控件數(shù)組類增加AddItem方法,該方法在控件數(shù)組類中添加成員;
4、為控件數(shù)組類增加RemoveItem方法,該方法在控件數(shù)組中刪除一個(gè)成員。
示例代碼:
Public Class ButtonArray
Inherits System.Collections.CollectionBase
Private ReadOnly ParentForm As System.Windows.Forms.Form
Public Sub New(ByVal pForm As System.Windows.Forms.Form)
ParentForm = pForm
End Sub
Default Public ReadOnly Property Item(ByVal index As Integer) As System.Windows.Forms.Button
Get
Return Me.List.Item(index) ' ButtonArray的List 屬性從CollectionBase 繼承
End Get
End Property
Public Sub AddItem()
Dim btnItem As New System.Windows.Forms.Button
Me.List.Add(btnItem)
ParentForm.Controls.Add(btnItem) '向窗體中增加控件
btnItem.Tag = Me.Count 'Count屬性從CollectionBase 繼承
btnItem.Top = Me.Count * 30
btnItem.Left = 200
btnItem.Text = "Button" Me.Count.ToString
AddHandler btnItem.Click, AddressOf btnItem_Click '綁定事件處理程序
End Sub
Public Sub AddItem(ByVal btnItem As System.Windows.Forms.Button)
Me.List.Add(btnItem)
AddHandler btnItem.Click, AddressOf btnItem_Click '綁定事件處理程序
End Sub
Public Sub RemoveItem()
If Me.Count 0 Then
ParentForm.Controls.Remove(Me(Me.Count - 1))
Me.List.RemoveAt(Me.Count - 1)
End If
End Sub
Public Sub btnItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'在這里編寫控件數(shù)組對點(diǎn)擊事件的響應(yīng)
'例如:
MsgBox("點(diǎn)擊:" sender.GetType().ToString CType(CType(sender, Button).Tag, String))
End Sub
End Class
使用創(chuàng)建的控件數(shù)組
在Form1中放置兩個(gè)按鈕Button1、Button2,分別測試控件數(shù)組的增添、刪除。
雙擊Form添加代碼:
Public Class Form1
Inherits System.Windows.Forms.Form
……Windows窗體設(shè)計(jì)器生成的代碼……
Dim Buttons As New ButtonArray(Me)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Buttons.AddItem()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Buttons.RemoveItem()
End Sub
End Class
其他的控件數(shù)組也可以用類似的方式來實(shí)現(xiàn)
例如 Label控件數(shù)組
LabelArray.vb代碼如下:
Public Class LabelArray
Inherits System.Collections.CollectionBase
Private ReadOnly ParentForm As System.Windows.Forms.Form
Public Sub New(ByVal pForm As System.Windows.Forms.Form)
ParentForm = pForm
End Sub
Default Public ReadOnly Property Item(ByVal index As Integer) As System.Windows.Forms.Label
Get
Return Me.List.Item(index) ' ButtonArray的List 屬性從CollectionBase 繼承
End Get
End Property
Public Sub AddItem(ByVal btnItem As System.Windows.Forms.Label)
Me.List.Add(btnItem)
AddHandler btnItem.Click, AddressOf btnItem_Click '綁定事件處理程序
End Sub
Public Sub btnItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'在這里編寫控件數(shù)組對點(diǎn)擊事件的響應(yīng)
'例如:
MsgBox("點(diǎn)擊:" sender.GetType().ToString CType(CType(sender, Label).Tag, String))
End Sub
End Class
使用創(chuàng)建的Label控件
在Form1中放置兩個(gè)按鈕Label1、Label2
雙擊Form添加代碼:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗體設(shè)計(jì)器生成的代碼 "
Public Sub New()
MyBase.New()
'該調(diào)用是 Windows 窗體設(shè)計(jì)器所必需的。
InitializeComponent()
'在 InitializeComponent() 調(diào)用之后添加任何初始化
'用來綁定label
BindArray()
End Sub
……Windows窗體設(shè)計(jì)器生成的其他代碼……
#End Region
Dim Labels As New LabelArray(Me)
Public Sub BindArray()
Me.Label1.Tag = "1111"
Me.Label2.Tag = "222"
Labels.AddItem(Me.Label1)
Labels.AddItem(Me.Label2)
End Sub
End Class
然后可以測試點(diǎn)擊兩個(gè)label可以顯示相應(yīng)的Tag的信息。
vb.NET中也有ADODB.Recordset,但我們一般用SqlDataReader來實(shí)現(xiàn)相同的效果。
Adodb.RecordSet在VB.Net中相對應(yīng)的是DataTable。這個(gè)東西比RecordSet靈活而且支持豐富的過濾及計(jì)算方法。
Private?ctrName?As?String?'控件名稱
Private?isClick?As?Boolean?'鼠標(biāo)點(diǎn)擊狀態(tài)
'注:如果已知點(diǎn)擊目標(biāo)控件的父控件,ctrParent變量可以不要。
Private?WithEvents?ctrParent?As?Control?'父控件
Private?Sub?ControlAMouseDown(sender?As?Object,?e?As?MouseEventArgs)?_
Handles?Button1.MouseDown
isClick?=?(e.Button?=?MouseButtons.Left?_
Or?e.Button?=?MouseButtons.Right)?'左鍵或右鍵按下
If?isClick?Then
Dim?ctr?As?Control?=?CType(sender,?Control)?'轉(zhuǎn)換Object為控件類型
ctrName?=?ctr.Name?'獲取控件名稱
ctrParent?=?ctr.Parent?'獲取控件的父控件
End?If
End?Sub
'增加這個(gè)父控件事件,是為了正確判別鼠標(biāo)彈起時(shí)是否已進(jìn)入指定目標(biāo)
Private?Sub?ParentMouseMove(sender?As?Object,?e?As?EventArgs)?_
Handles?ctrParent.MouseMove?'如果已取消ctrParent變量,改為相應(yīng)的父控件
If?isClick?Then?isClick?=?False?'點(diǎn)擊狀態(tài)關(guān)閉
End?Sub
Private?Sub?ControlBMouseUp(sender?As?Object,?e?As?EventArgs)?_
Handles?Button2.MouseEnter
If?isClick?Then?'如果點(diǎn)擊狀態(tài)為打開
Dim?ctr?As?Control?=?CType(sender,?Control)?'轉(zhuǎn)換Object為控件類型
MsgBox(ctrName??"?|?"??ctr.Name)?'彈出消息顯示結(jié)果
End?If
End?Sub
如果是.NET平臺的,你可以使用ComponentOne Enterprise 全功能.NET控件集中的布局控件實(shí)現(xiàn)。也可以試試ComponentOne的LinearGauge 控件,這是一個(gè)線性儀表器,顯示為一個(gè)值和象征參考值的可選范圍。線性儀表盤(LinearGauge)控件提供了一個(gè) ShowText 屬性,用來決定哪些值以文本顯示。
》》傳送門:網(wǎng)頁鏈接