在VB6中有一个名为Control Arrays的功能,您可以在其中为控件命名相同的名称并为其提供索引值。这允许您通过循环控件和设置每个值来设置值。在VB .NET中我无法创建控件数组,有人可以为我提供类似的解决方案。
答案 0 :(得分:13)
以下是我为其他内容编写的示例,该示例演示了如何执行类似操作并显示如何执行处理程序。这使得10x10网格的按钮在您单击时变为红色。
Dim IsCreated(99) As Boolean
Dim Buttons As New Dictionary(Of String, Button)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For i As Integer = 0 To 99
Dim B As New Button
Me.Controls.Add(B)
B.Height = 30
B.Width = 40
B.Left = (i Mod 10) * 41
B.Top = (i \ 10) * 31
B.Text = Chr((i \ 10) + Asc("A")) & i Mod 10 + 1
Buttons.Add(B.Text, B)
B.Tag = i
AddHandler B.Click, AddressOf Button_Click
Next
End Sub
Private Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim B As Button = sender
IsCreated(B.Tag) = True
B.BackColor = Color.Red
End Sub
答案 1 :(得分:9)
避免使用建议的迭代方法,除非您的表单非常简单,否则您将获得相当随机的控件集合。只需在代码中声明控件数组并在表单构造函数中初始化它。像这样:
Public Class Form1
Private OrderNumbers() As TextBox
Public Sub New()
InitializeComponent()
OrderNumbers = New TextBox() {TextBox1, TextBox2}
End Sub
End Class
您现在可以像在VB6中那样处理OrderNumbers。
答案 2 :(得分:3)
也许这更简单。为了创建一个控件数组,我将控件数组声明放在一个模块中。例如,如果我有一个带有三个TextBox的Form,并且我希望TextBoxes成为名为'mytext'的控件数组的一部分,我将模块中的控制数组声明如下:
Module Module1
Public mytext() As TextBox = {Form1.TextBox1, Form1.TextBox2, Form1.TextBox3}
End Module
并且,我使用控件数组中的TextBoxes,如下所示:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
mytext(0).Text = "Hello"
mytext(1).Text = "Hi"
mytext(2).Text = "There"
End Sub
End Class
你甚至可以像在VB6中一样循环控制数组:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 2
mytext(i).Text = i + 1
Next
End Sub
End Class
使用模块的好处是TextBoxes甚至不需要采用相同的形式。
答案 3 :(得分:1)
使用Winforms,您可以这样做:
myForm.Controls _
.OfType(Of TextBox) _
.OrderBy(Function(c) c.Name) _
.Where(Function(c) c.Name.StartsWith("somePrefix")) _
.ToArray()
在表单上,您可以为文本框somePrefix1
,somePrefix2
等命名。
这是old article,但它可以为您提供更多信息。顶级方法非常简单。
答案 4 :(得分:1)
您的表单或PanelControl或其他任何可以包含子控件的内容都将包含一个名为Controls
的属性。
您可以使用
遍历控件中的所有文本框'创建一个TextBox的列表,就像一个数组但更好 将myTextBoxControls变暗为新列表
For Each uxControl As UserControl in MyFormName.Controls
If TypeOf(uControl) is TextBox
myTextBoxControls.Add(uControl)
End IF
Next
现在您可以使用可以进行迭代的集合了。
您可以使用EditValue
属性访问TextBoxes值。
在看了一下你想要做些什么之后。
您可能希望使用前缀命名所有控件,暂时说abc
。
For Each uxControl As UserControl in MyFormName.Controls
If TypeOf(uControl) is TextBox Then
Dim tbControl As TextBox = DirectCast(uControl, TextBox)
If tbControl.Name.StartsWith("abc") Then
tbControl.EditValue = "the Value you want to initialize"
End If
End If
Next
答案 5 :(得分:0)
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Dim a() As Control = GetControls("textbox")
For Each c As TextBox In a
c.Text = c.Name
Next
End Sub
Private Function GetControls(typeOfControl As String) As Control()
Dim allControls As New List(Of Control)
'this loop will get all the controls on the form
'no matter what the level of container nesting
'thanks to jmcilhinney at vbforums
Dim ctl As Control = Me.GetNextControl(Me, True)
Do Until ctl Is Nothing
allControls.Add(ctl)
ctl = Me.GetNextControl(ctl, True)
Loop
'now return the controls you want
Return allControls.OrderBy(Function(c) c.Name). _
Where( _
Function(c) (c.GetType.ToString.ToLower.Contains(typeOfControl.ToLower) AndAlso _
c.Name.Contains("Box")) _
).ToArray()
End Function
答案 6 :(得分:0)
所以这是没有向VB.NET过渡的功能之一 - 确切地说:-(但是,您可以通过.NET中的两种不同机制完成VB6中的大部分工作:循环通过控件收集和处理控制事件。
通过控件集循环
在VB.NET中,每个表单和控件容器都有一个控件集合。这是一个集合,您可以循环,然后在控件上执行操作,如设置值。
Dim myTxt As TextBox
For Each ctl As Control In Me.Controls
If TypeOf ctl Is TextBox Then
myTxt = CType(ctl, TextBox)
myTxt.Text = "something"
End If
Next
在此代码示例中,您将迭代控件集合,以测试返回对象的类型。如果找到文本框,请将其强制转换为文本框,然后对其执行操作。
处理控制事件
您还可以使用一个事件处理程序处理多个控件上的事件,就像使用VB6中的控件数组一样。为此,您将使用Handles关键字。
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
Dim myTxt As TextBox = CType(sender, TextBox)
MessageBox.Show(myTxt.Text)
End Sub
此处的关键是事件处理程序末尾的句柄关键字。您可以使用逗号分隔出要处理的各种控件和事件。确保您正在处理具有相同事件声明的控件。如果您想知道每个事件的发件人是什么,这里就是其中一个用途。将sender参数转换为您正在使用的控件类型,并将其分配给局部变量。然后,您将能够访问和操作触发事件的控件,就像您在VB6中指定并索引数组一样。
使用这两种技术,您可以在VB6中复制控制数组的功能。祝你好运。