What's the simplest .NET equivalent of a VB6 control array?

后端 未结 9 1599
时光取名叫无心
时光取名叫无心 2020-12-11 03:42

Maybe I just don\'t know .NET well enough yet, but I have yet to see a satisfactory way to implement this simple VB6 code easily in .NET (assume this code is on a form with

相关标签:
9条回答
  • 2020-12-11 03:46

    Make a generic list of textboxes:

    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;
    }
    
    0 讨论(0)
  • 2020-12-11 03:46

    VisualBasic .NET's compatibility library contains strong typed control arrays. This is what the upgrade wizard uses to replace the current VB6 control arrays.

    However, A control array in VB6 is just a collection of objects with VB6 doing some syntax magic on the surface. In the .NET world, by removing this, they are forcing better practices.

    In closing, with the advent of generics, there is nothing stopping you from using

    List<YourControl> MyControlArray.
    
    0 讨论(0)
  • 2020-12-11 03:50

    There is no real 1 to 1 analog in .Net. Sure, you can make arrays or lists of controls of a specific type, but there's nothing that will do that for you automatically.

    However, I've never seen a control array that couldn't be refactored in .Net to something better. A case in point is your example. In the scenario you posted, you're using control arrays to pair a button up with a textbox. In .Net, you would probably do this with a custom control. The custom control would consist of a button, a textbox, and maybe a shared/static timer. The form uses several instances of this custom control. You implement the logic needed for the control once, and it's isolated to it's own source file which can be tracked and edited in source control without requiring a merge with the larger form class, or easily re-used on multiple forms or even in multiple projects. You also don't have to worry about making sure the command button index matches up with the textbox index.

    Using a custom control for this instead of a control array is loosely analogous to using class to group data instead of an array, in that you get names instead of indexes.

    0 讨论(0)
  • 2020-12-11 03:50

    I know that my answer is quite late, but I think I found the solution. I'm not the only former VB6 developer who has struggled with this limitation of VS. Long ago, I tried to migrate a CRM that I designed, but I failed because I had a tough dependency of control arrays (hundreds in one form). I read many forums and I was able to write this simple code:

    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
    

    C#:

    //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();
    }
    

    There are 3 points to take in consideration:

    1. A List will help you to emulate the large collection of controls.
    2. typeof(Control) will help you define the type of the control to add in the list.
    3. While you keep the "index" as the last character (textBox1, textBox2, ..., textBoxN) you can create a logical order.

    Example in design mode:

    Running:

    A similar logic could be potentially used in other technologies like WPF, ASP.NET (Web Forms) or Xamarin (Forms) -in my opinion-. I hope this piece of code is going to help more programmers in the future.

    0 讨论(0)
  • 2020-12-11 03:51

    The two main benefits of control arrays in VB6 were: (1) They provided a way for you to iterate through a collection of controls (2) They allowed you to share events between controls

    (1) can be accomplished in .Net using an array of controls (2) can be accomplished by having one event handle multiple controls (the syntax is a little different because you use the sender argument instead of myArray(index)).

    One nice thing about .Net is that these features are decoupled. So for instance you can have controls that share events even if they aren't part of an array and have different names and even a different type. And you can iterate through a collection of controls even if they have totally different events.

    0 讨论(0)
  • 2020-12-11 04:02

    Make an array of controls.

    TextBox[] textboxes = new TextBox[] {
        textBox1,
        textBox2,
        textBox3
    };
    
    0 讨论(0)
提交回复
热议问题