什么';是VB6控件数组最简单的.NET等价物吗?

什么';是VB6控件数组最简单的.NET等价物吗?,.net,vb6,vb6-migration,control-array,.net,Vb6,Vb6 Migration,Control Array,也许我还不太了解.NET,但我还没有找到一种令人满意的方法在.NET中轻松实现这个简单的VB6代码(假设此代码位于一个表单上,在数组Command1()中有N个commandbutton,在数组Text1()中有N个textbox): 我知道这不是很有用的代码,但它演示了在VB6中使用控件数组的简易性。在C#或VB.NET中最简单的等价物是什么?创建一个控件数组 TextBox[] textboxes = new TextBox[] { textBox1, textBox2,

也许我还不太了解.NET,但我还没有找到一种令人满意的方法在.NET中轻松实现这个简单的VB6代码(假设此代码位于一个表单上,在数组Command1()中有N个commandbutton,在数组Text1()中有N个textbox):


我知道这不是很有用的代码,但它演示了在VB6中使用控件数组的简易性。在C#或VB.NET中最简单的等价物是什么?

创建一个控件数组

TextBox[] textboxes = new TextBox[] {
    textBox1,
    textBox2,
    textBox3
};

VB.NET所做的另一件好事是拥有一个处理多个控件的事件处理程序:

Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ 
        Handles TextBox1.TextChanged, _

        TextBox2.TextChanged, _

        TextBox3.TextChanged

End Sub

VisualBasic.NET的兼容性库包含强类型控件数组。这就是升级向导用来替换当前VB6控件阵列的内容

然而,VB6中的控件数组只是对象的集合,VB6在表面上执行一些语法魔术。在.NET世界中,通过删除此项,他们正在强制实施更好的实践

最后,随着泛型的出现,没有什么可以阻止您使用

List<YourControl> MyControlArray.
列出MyControlArray。
有两个方面

NET很容易支持控件数组,VB6只能使用变通方法,因为否则,连接事件非常困难。在.NET中,动态连接事件很容易

但是,.NET表单设计器不支持控件数组,原因很简单:控件数组是在运行时创建/扩展的。如果您知道在编译时需要多少个控件(推理是这样的),那么您可以为它们指定不同的名称,而不是将它们放入数组中

我知道这不是很有用的代码

这正是重点。如果一个功能是无用的,为什么还要有它呢

如果需要,还可以按名称访问控件,结果如下:

Private Sub Command_Click(sender As Object, e As EventArgs) Handles Command1.Click, Command2.Click …
    Dim name As String = DirectCast(sender, Control).Name
    Dim index As Integer = Integer.Parse(name.Substring("Command".Length))
    Controls(String.Format("Text {0}", index)).Text = Timer.Value.ToString()
End Sub

制作文本框的通用列表:

var textBoxes = new List<TextBox>();

// Create 10 textboxes in the collection
for (int i = 0; i < 10; i++)
{
    var textBox = new TextBox();
    textBox.Text = "Textbox " + i;
    textBoxes.Add(textBox);
}

// Loop through and set new values on textboxes in collection
for (int i = 0; i < textBoxes.Count; i++)
{
    textBoxes[i].Text = "New value " + i;
    // or like this
    var textBox = textBoxes[i];
    textBox.Text = "New val " + i;
}
var textboxs=新列表();
//在集合中创建10个文本框
对于(int i=0;i<10;i++)
{
var textBox=新的textBox();
textBox.Text=“textBox”+i;
textboxs.Add(textBox);
}
//在集合中的文本框上循环并设置新值
对于(int i=0;i
同一个单击事件可以处理.Net中多个按钮的按钮按下。然后,您可以在文本框中添加要在标记属性中查找的内容

Private Sub AllButton_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
  Dim c As Control = CType(sender, Control)
  Dim t As TextBox = FindControl(CType(c.Tag, String))
  If t Is Not Nothing Then
     t.Text = "Clicked"
  End If
End Sub
Net中没有真正的1对1模拟。当然,您可以创建特定类型的控件的数组或列表,但没有任何东西可以自动为您创建这些控件

然而,我从未见过一个控件数组不能在.Net中重构成更好的东西。你的例子就是一个恰当的例子。在您发布的场景中,您使用控件数组将按钮与文本框配对。在.Net中,您可能会使用自定义控件来执行此操作。自定义控件将由一个按钮、一个文本框和一个共享/静态计时器组成。表单使用此自定义控件的多个实例。您只需实现一次控件所需的逻辑,并且它与它自己的源文件隔离,可以在源代码管理中跟踪和编辑该源文件,而无需与更大的表单类合并,也可以轻松地在多个表单甚至多个项目中重复使用。您也不必担心确保命令按钮索引与文本框索引匹配


为此使用自定义控件而不是控件数组大致类似于使用类来分组数据而不是数组,因为您可以获取名称而不是索引

VB6中控制阵列的两个主要优点是: (1) 它们为您提供了一种遍历控件集合的方法 (2) 它们允许您在控件之间共享事件

(1) 可以使用一组控件在.Net中完成 (2) 可以通过一个事件处理多个控件来实现(语法略有不同,因为您使用的是
sender
参数,而不是
myArray(index)


Net的一个优点是这些特性是解耦的。例如,您可以拥有共享事件的控件,即使它们不是数组的一部分,并且具有不同的名称,甚至是不同的类型。你可以遍历一组控件,即使它们有完全不同的事件。

我知道我的答案很晚,但我想我找到了解决方案。我并不是唯一一位曾经努力克服VS限制的前VB6开发人员。很久以前,我曾尝试迁移我设计的CRM,但失败了,因为我对控制阵列(一种形式中有数百个)有很强的依赖性。我阅读了许多论坛,并能够编写以下简单代码:

VB.NET:

'To declare the List of controls
Private textBoxes As List(Of TextBox) = New List(Of TextBox)()

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    'To get all controls in the form
    For Each control In Controls

        'To search for the specific type that you want to create the array 
        If control.[GetType]() = GetType(TextBox) Then
            textBoxes.Add(CType(control, TextBox))
        End If
    Next

    'To sort the labels by the ID
    textBoxes = textBoxes.OrderBy(Function(x) x.Name).ToList()
End Sub
//To declare the List of controls
private List<TextBox> textBoxes = new List<TextBox>();
private void Form1_Load(object sender, EventArgs e)
{
    //To get all controls in the form
    foreach (var control in Controls)
    {
        //To search for the specific type that you want to create the array 
        if (control.GetType() == typeof(TextBox))
        {
            //To add the control to the List
            textBoxes.Add((TextBox)control);
        }
    }

    //To sort the labels by the ID
    textBoxes = textBoxes.OrderBy(x => x.Name).ToList();
}
C#:

'To declare the List of controls
Private textBoxes As List(Of TextBox) = New List(Of TextBox)()

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    'To get all controls in the form
    For Each control In Controls

        'To search for the specific type that you want to create the array 
        If control.[GetType]() = GetType(TextBox) Then
            textBoxes.Add(CType(control, TextBox))
        End If
    Next

    'To sort the labels by the ID
    textBoxes = textBoxes.OrderBy(Function(x) x.Name).ToList()
End Sub
//To declare the List of controls
private List<TextBox> textBoxes = new List<TextBox>();
private void Form1_Load(object sender, EventArgs e)
{
    //To get all controls in the form
    foreach (var control in Controls)
    {
        //To search for the specific type that you want to create the array 
        if (control.GetType() == typeof(TextBox))
        {
            //To add the control to the List
            textBoxes.Add((TextBox)control);
        }
    }

    //To sort the labels by the ID
    textBoxes = textBoxes.OrderBy(x => x.Name).ToList();
}
//声明控件列表的步骤
私有列表文本框=新列表();
私有void Form1\u加载(对象发送方、事件参数e)
{
//获取窗体中的所有控件
foreach(控件中的var控件)
{
//搜索要创建数组的特定类型
if(control.GetType()==typeof(TextBox))
{
//将控件添加到列表中的步骤
添加((TextBox)控件);
}
}
//按ID对标签排序的步骤
textboxs=textboxs.OrderBy(x=>x.Name.ToList();
}
有三点需要考虑:

  • 列表将帮助您模拟大量控件集合
  • typeof(控件)
    将帮助您定义要添加到列表中的控件类型
  • “index”保留为最后一个字符(textBox1、textBox2、…、textBoxN)时,可以创建逻辑顺序
  • 设计模式下的示例:

    跑步:

    'To declare the List of controls
    Private textBoxes As List(Of TextBox) = New List(Of TextBox)()
    
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        'To get all controls in the form
        For Each control In Controls
    
            'To search for the specific type that you want to create the array 
            If control.[GetType]() = GetType(TextBox) Then
                textBoxes.Add(CType(control, TextBox))
            End If
        Next
    
        'To sort the labels by the ID
        textBoxes = textBoxes.OrderBy(Function(x) x.Name).ToList()
    End Sub
    
    //To declare the List of controls
    private List<TextBox> textBoxes = new List<TextBox>();
    private void Form1_Load(object sender, EventArgs e)
    {
        //To get all controls in the form
        foreach (var control in Controls)
        {
            //To search for the specific type that you want to create the array 
            if (control.GetType() == typeof(TextBox))
            {
                //To add the control to the List
                textBoxes.Add((TextBox)control);
            }
        }
    
        //To sort the labels by the ID
        textBoxes = textBoxes.OrderBy(x => x.Name).ToList();
    }
    

    类似的逻辑可以潜在地用于其他技术,如WPF、ASP.N