views:

1996

answers:

8

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 N CommandButtons in array Command1() and N TextBoxes in array Text1()):

Private Sub Command1_Click(Index As Integer)

   Text1(Index).Text = Timer

End Sub

I know it's not very useful code, but it demonstrates the ease with which control arrays can be used in VB6. What is the simplest equivalent in C# or VB.NET?

A: 

Make an array of controls.

TextBox[] textboxes = new TextBox[] {
    textBox1,
    textBox2,
    textBox3
};
Lasse V. Karlsen
+2  A: 

Another nice thing that VB .NET does is having a single event handler that handles multiple controls:

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

        TextBox2.TextChanged, _

        TextBox3.TextChanged

End Sub
Mark Biek
+1  A: 

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.
FlySwat
+2  A: 

There are two aspects.

.NET readily supports arrays of controls, VB6 just had to use a workaround because otherwise, wiring up events was really hard. In .NET, wiring up events dynamically is easy.

However, the .NET form designer does not support control arrays for a simple reason: arrays of controls are created/extended at run time. If you know how many controls you need at compile time (the reasoning goes) then you give them different names and don't put them in an array.

I know it's not very useful code

That's exactly the point. Why have a feature if it's useless?

If needed, you can also access a control by name, resulting in something like this:

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
Konrad Rudolph
I was referring to my example when I said, "it's not very useful code". I have found control arrays to be extremely useful in VB6, which is why I posed the question. I found your proposed solution pretty clever, but it just seems like a bit of a kludge.
raven
+2  A: 

Make a generic list of textboxes:

List<TextBox> textBoxes = new List<TextBox>();
// Create 10 textboxes in the collection
for (int i = 0; i < 10; i++)
{
    TextBox textBox = new TextBox();
    textBox.Text = "Textbox " + i;
    textBoxes.Add(textBox);
}
// Loop through and set new values on textboxes i collection
for (int i = 0; i < textBoxes.Count; i++)
{
    textBoxes[i].Text = "New value " + i;
    // or like this
    TextBox textBox = textBoxes[i];
    textBox.Text = "New val " + i;
}
Seb Nilsson
A: 

The same click event can handle the button presses from multiple buttons in .Net. You could then add the the text box to find in the Tag property?

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
samjudson
+3  A: 

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.

Joel Coehoorn
A: 

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.

Tim Goodman