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

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

VB.Net中创建使控件数组

 

一、           关于“控件数组

控件数组”是VB6中一项简单而实用的技术,通过对控件的简单拷贝、复制,开发者可以指定一组控件,这些控件具有相同的类型和名称,共享事件集。使用控件数组可以:1、允许多个控件共享同一个事件句柄;2、提供了运行期间增加一个控件的机制;3、提供了一种方便的组合控件的方法。

VB.Net中,控件数组的创建不再通过VB6中设计时对控件的简单拷贝、复制而实现。VB .Net 的事件模型允许任何事件处理程序处理来自多个控件的事件,这使我们能够编程创建属于不同类型但共享相同事件的控件组。

 

二、           VB.Net中编程创建控件数组

下面我们创建一个Button类型控件数组

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

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

3、为控件数组类增加AddItem方法,该方法在控件数组类中添加成员;

4、为控件数组类增加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 * 30

        btnItem.Left = 200

        btnItem.Text = "Button" & Me.Count.ToString

        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)

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

        '例如:

        MsgBox("点击:" & sender.GetType().ToString & CType(CType(sender, Button).Tag, String))

    End Sub

End Class

 

 

三、           使用创建的控件数组

Form1中放置两个按钮Button1Button2,分别测试控件数组的增添、删除。

双击Form添加代码:

Public Class Form1

    Inherits System.Windows.Forms.Form

 

……Windows窗体设计器生成的代码……

    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

 

可见,与VB6中实现控件数组的方法相比,VB.Net似乎复杂了一些,但同时也使开发人员在编程时有了更大的灵活度。

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值