您现在的位置是:首页 >

labview保存到数组 在VB.Net中创建使用控件数组

火烧 2021-12-11 10:55:41 1061
在VB.Net中创建使用控件数组 一 首先创建一个Butto 类型控件数组    .创建 Wi dow 应用程序 类型的工程 添加名为Butto Array的类 并使该类继承 Sy tem Colle

在VB.Net中创建使用控件数组  

一 首先创建一个Button类型控件数组

   .创建 Windows应用程序 类型的工程 添加名为ButtonArray的类 并使该类继承 System Collection CollectionBase 类 System Collections CollectionBase类是 NET框架类库中为集合操作提供抽象的基类 通过对它的继承可以为我们的ButtonArray类具备集合增加 删除 索引的功能

   .为ButtonArray类添加ParentForm属性 即控件组所在窗体 创建初始化函数(构造函数)

   .为控件数组类增加AddItem方法 该方法在控件数组类中添加成员

   .为控件数组类增加RemoveItem方法 该方法在控件数组中删除一个成员

示例代码

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 *

btnItem Left =

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 SubPublic Sub RemoveItem()

If Me Count > Then

ParentForm Controls Remove(Me(Me Count ))

Me List RemoveAt(Me Count )

End If

End Sub

Public Sub btnItem_Click(ByVal sender As Object ByVal e As System EventArgs)

在这里编写控件数组对点击事件的响应

例如

MsgBox( 点击 & sender GetType() ToString & CType(CType(sender Button) Tag String))

End Sub

End Class

二 使用创建的控件数组

  在Form 中放置两个按钮Button Button 分别测试控件数组的增添 删除

双击Form添加代码

Public Class Form Inherits System Windows Forms Form

Windows窗体设计器生成的代码

Dim Buttons As New ButtonArray(Me)

Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click

Buttons AddItem()

End Sub

Private Sub Button _Click(ByVal sender As System Object ByVal e As System EventArgs) Handles Button Click

Buttons RemoveItem()

End Sub

End Class

其他的控件数组也可以用类似的方式来实现 例如 Label控件数组

LabelArray vb代码如下 Public Class LabelArrayInherits 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 继承

labview保存到数组 在VB.Net中创建使用控件数组

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)

在这里编写控件数组对点击事件的响应

例如

MsgBox( 点击 & sender GetType() ToString & CType(CType(sender Label) Tag String))

End SubEnd Class

三 使用创建的Label控件  在Form 中放置两个按钮Label Label

双击Form添加代码

Public Class Form

Inherits System Windows Forms Form

#Region Windows 窗体设计器生成的代码

Public Sub New()MyBase New()

该调用是 Windows 窗体设计器所必需的 InitializeComponent()

在 InitializeComponent() 调用之后添加任何初始化 用来绑定labelBindArray()

End SubWindows窗体设计器生成的其他代码

#End RegionDim Labels As New LabelArray(Me)Public Sub BindArray()Me Label Tag = Me Label Tag = Labels AddItem(Me Label )Labels AddItem(Me Label )End SubEnd Class

lishixinzhi/Article/program/net/201311/15141  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码