vb.net控件组 vb列表控件
vb.net有没有类似vb6.0控件数组的功能
可以实现
成都创新互联公司是一家朝气蓬勃的网站建设公司。公司专注于为企业提供信息化建设解决方案。从事网站开发,网站制作,网站设计,网站模板,微信公众号开发,软件开发,微信小程序开发,十余年建站对成都白乌鱼等多个方面,拥有丰富的网站维护经验。
首先创建一个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 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)
'在这里编写控件数组对点击事件的响应
'例如:
MsgBox("点击:" sender.GetType().ToString CType(CType(sender, Button).Tag, String))
End Sub
End Class
使用创建的控件数组
在Form1中放置两个按钮Button1、Button2,分别测试控件数组的增添、删除。
双击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
其他的控件数组也可以用类似的方式来实现
例如 Label控件数组
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)
'在这里编写控件数组对点击事件的响应
'例如:
MsgBox("点击:" sender.GetType().ToString CType(CType(sender, Label).Tag, String))
End Sub
End Class
使用创建的Label控件
在Form1中放置两个按钮Label1、Label2
双击Form添加代码:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
'用来绑定label
BindArray()
End Sub
……Windows窗体设计器生成的其他代码……
#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
然后可以测试点击两个label可以显示相应的Tag的信息。
vb.net中如何将多个单选按钮设为一组
添加一个OPTION控件,复制-粘贴,建立 控件组
比如复制6个
代码如下
Private Sub Command1_Click()
For i = 0 To 5
If Option1(i).Value = True Then MsgBox Option1(i).Caption "被选中"
Next
End Sub
在VB.net里定义的控件数组怎么定义事件
Imports System.Text
Imports System.Windows
Imports System.Windows.Forms
Public Class Netin '类 名
'Inherits Forms.Form
Public Shared Netin As New Form
Public components As ComponentModel.IContainer
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
DebuggerStepThrough()
Public Sub InitializeComponent()
Me.components = New ComponentModel.Container()
Dim resources As ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(bjt))
Me.TextBox1 = New TextBox(Me.components)
Me.Button1 = New Button(Me.components)
'For … Next
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.m = New ContextMenuStrip(Me.components)
Me.m1.Text = "菜单项1"
'设置并添加相关控件,(窗体)Netin 初始化。
End Sub
Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
'加载后显示窗体Netin。
End Sub
Public Sub ObjClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click,Button2.Click,Button3.Click,TextBox1.Click
MessageBox.Show("你按了鼠标")
End Sub
Public WithEvents TextBox1 As TextBox
Public WithEvents Button1 As Button
Public WithEvents Button2 As Button
Public WithEvents Button3 As Button
Friend WithEvents m As ContextMenuStrip
Friend WithEvents m1 As ToolStripMenuItem
End Class
Class MainWindow
Netin.Show()
Netin.Button2.Text = "Main"
……
VB中控件数组在VB.NET中用法,请给一个例子
VB.net中没有控件数组的说法。
替代方法:
·创建一个控件的类型数组: Button[],将所有的button控件创建好后存进去,可以遍历它;
·或将所有要遍历的控件放在一个容器如Panel中,以后遍历这个容器的子控件即可。
----------
你的情况,推荐放在容器中。比如下面就是一个遍历容器的控件,然后找出所有的文本框并修改内容的程序:
//pn 是个 Panel 控件
foreach (Control item in pn.Controls)
{
if (typeof(TextBox) == item.GetType())
{
((TextBox)item).Text = "我是动态修改的!";
}
}
vb.net 怎样创建控件数组?
Dim Str(20) As String '20数据元素
Dim Text(20) As TextBox
Dim n As Integer = 0
For Each i In Text
i = New TextBox '实例化
i.Location = New Point(50, n * 20)
Me.Controls.Add(i) '添加窗体
Dim Index As Integer = i.TabIndex '没index属性
Str(n) = Index
i.Text = Str(n) 'str数组值赋值给文本框
n += 1
Next
文章标题:vb.net控件组 vb列表控件
网站URL:http://azwzsj.com/article/hiijgi.html