Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the VB.NET control array

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use the VB.NET control array, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

About the array of VB.NET controls

Control array "is a simple and practical technology in VB6. By simply copying and copying controls, developers can specify a set of controls that have the same type and name and share a set of events. With control array, you can:"

1. Allow multiple controls to share the same event handle

2. Provides a mechanism for adding a control at run time

3. A convenient method of combining controls is provided.

The creation of an array of VB.NET controls is no longer achieved by simply copying and copying controls at design time in VB6. The event model of VB .NET allows any event handler to handle events from multiple controls, which enables us to programmatically create groups of controls that belong to different types but share the same event.

Create an array of VB.NET controls

Let's create an array of Button type controls:

1. Create a project of type "Windows Application", add a class named ButtonArray, and make it inherit the System.Collection.CollectionBase class. System.Collections.CollectionBase class is the base class that provides abstraction for collection operation in .NET Framework class library. Through inheriting it, we can add, delete and index collections for our ButtonArray class.

2. Add the ParentForm attribute to the ButtonArray class, that is, the form where the control group is located, and create an initialization function (constructor).

3. Add an AddItem method to the control array class, which adds members to the control array class

4. Add a RemoveItem method to the control array class, which removes a member from the control array.

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)

'The List property of ButtonArray inherits from CollectionBase

End Get

End Property

Public Sub AddItem ()

Dim btnItem As New System.Windows.Forms.Button ()

Me.List.Add (btnItem)

ParentForm.Controls.Add (btnItem)

'add controls to the form

The btnItem.Tag = Me.Count'Count attribute inherits from CollectionBase

BtnItem.Top = Me.Count * 30

BtnItem.Left = 200

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

AddHandler btnItem.Click, AddressOf btnItem_Click

'bind event handlers

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)

'write the control array's response to the click event here

'for example:

MsgBox ("click:" & sender.GetType (). ToString &

CType (CType (sender, Button). Tag, String))

End Sub

End Class

Thank you for reading this article carefully. I hope the article "how to use VB.NET Control Array" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report