<p>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()):</p>

<pre><code>Private Sub Command1_Click(Index As Integer)

   Text1(Index).Text = Timer

End Sub
</code></pre>

<p>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?</p>

[{'answer_id': 39544, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 0, 'selected': False, 'text': '<p>Make an array of controls.</p>\n\n<pre><code>TextBox[] textboxes = new TextBox[] {\n    textBox1,\n    textBox2,\n    textBox3\n};\n</code></pre>\n'}, {'answer_id': 39548, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 2, 'selected': False, 'text': '<p>Another nice thing that VB .NET does is having a single event handler that handles multiple controls:</p>\n\n<pre><code>Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ \n        Handles TextBox1.TextChanged, _\n\n        TextBox2.TextChanged, _\n\n        TextBox3.TextChanged\n\nEnd Sub\n</code></pre>\n'}, {'answer_id': 39552, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 1, 'selected': False, 'text': "<p>VisualBasic .NET's compatibility library contains strong typed control arrays. This is what the upgrade wizard uses to replace the current VB6 control arrays.</p>\n\n<p>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.</p>\n\n<p>In closing, with the advent of generics, there is nothing stopping you from using </p>\n\n<pre><code>List&lt;YourControl&gt; MyControlArray.\n</code></pre>\n"}, {'answer_id': 39553, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 2, 'selected': False, 'text': '<p>There are two aspects.</p>\n\n<p>.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.</p>\n\n<p>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.</p>\n\n<blockquote>\n  <p>I know it\'s not very useful code</p>\n</blockquote>\n\n<p>That\'s exactly the point. Why have a feature if it\'s useless?</p>\n\n<p>If needed, you can also access a control by name, resulting in something like this:</p>\n\n<pre><code>Private Sub Command_Click(sender As Object, e As EventArgs) Handles Command1.Click, Command2.Click …\n    Dim name As String = DirectCast(sender, Control).Name\n    Dim index As Integer = Integer.Parse(name.Substring("Command".Length))\n    Controls(String.Format("Text {0}", index)).Text = Timer.Value.ToString()\nEnd Sub\n</code></pre>\n'}, {'answer_id': 39556, 'author': 'Seb Nilsson', 'author_id': 2429, 'author_profile': 'https://Stackoverflow.com/users/2429', 'pm_score': 4, 'selected': True, 'text': '<p>Make a generic list of textboxes:</p>\n\n<pre><code>var textBoxes = new List&lt;TextBox&gt;();\n\n// Create 10 textboxes in the collection\nfor (int i = 0; i &lt; 10; i++)\n{\n    var textBox = new TextBox();\n    textBox.Text = "Textbox " + i;\n    textBoxes.Add(textBox);\n}\n\n// Loop through and set new values on textboxes in collection\nfor (int i = 0; i &lt; textBoxes.Count; i++)\n{\n    textBoxes[i].Text = "New value " + i;\n    // or like this\n    var textBox = textBoxes[i];\n    textBox.Text = "New val " + i;\n}\n</code></pre>\n'}, {'answer_id': 39570, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 0, 'selected': False, 'text': '<p>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?</p>\n\n<pre><code>Private Sub AllButton_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click\n  Dim c As Control = CType(sender, Control)\n  Dim t As TextBox = FindControl(CType(c.Tag, String))\n  If t Is Not Nothing Then\n     t.Text = "Clicked"\n  End If\nEnd Sub\n</code></pre>\n'}, {'answer_id': 39614, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 2, 'selected': False, 'text': "<p>There is no <em>real</em> 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.</p>\n\n<p>However, I've never seen a control array that couldn't be refactored in .Net to something <em>better</em>.  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.</p>\n\n<p>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.  </p>\n"}, {'answer_id': 2099212, 'author': 'Tim Goodman', 'author_id': 254364, 'author_profile': 'https://Stackoverflow.com/users/254364', 'pm_score': 0, 'selected': False, 'text': "<p>The two main benefits of control arrays in VB6 were:\n(1) They provided a way for you to iterate through a collection of controls\n(2) They allowed you to share events between controls</p>\n\n<p>(1) can be accomplished in .Net using an array of controls\n(2) can be accomplished by having one event handle multiple controls (the syntax is a little different because you use the <code>sender</code> argument instead of <code>myArray(index)</code>).</p>\n\n<p>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.</p>\n"}, {'answer_id': 57372785, 'author': 'Federico Navarrete', 'author_id': 1928691, 'author_profile': 'https://Stackoverflow.com/users/1928691', 'pm_score': 0, 'selected': False, 'text': '<p>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 on control arrays (hundreds in one form). I read many forums and I was able to write this simple code:</p>\n<p><strong>VB.NET:</strong></p>\n<pre><code>\'To declare the List of controls\nPrivate textBoxes As List(Of TextBox) = New List(Of TextBox)()\n\nPrivate Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)\n    \'To get all controls in the form\n    For Each control In Controls\n\n        \'To search for the specific type that you want to create the array \n        If control.[GetType]() = GetType(TextBox) Then\n            textBoxes.Add(CType(control, TextBox))\n        End If\n    Next\n\n    \'To sort the labels by the ID\n    textBoxes = textBoxes.OrderBy(Function(x) x.Name).ToList()\nEnd Sub\n</code></pre>\n<p><strong>C#:</strong></p>\n<pre><code>//To declare the List of controls\nprivate List&lt;TextBox&gt; textBoxes = new List&lt;TextBox&gt;();\nprivate void Form1_Load(object sender, EventArgs e)\n{\n    //To get all controls in the form\n    foreach (var control in Controls)\n    {\n        //To search for the specific type that you want to create the array \n        if (control.GetType() == typeof(TextBox))\n        {\n            //To add the control to the List\n            textBoxes.Add((TextBox)control);\n        }\n    }\n\n    //To sort the labels by the ID\n    textBoxes = textBoxes.OrderBy(x =&gt; x.Name).ToList();\n}\n</code></pre>\n<p>There are 3 points to take into consideration:</p>\n<ol>\n<li>A <code>List</code> will help you to emulate the large collection of controls.</li>\n<li><code>typeof(Control)</code> will help you define the type of the control to add to the list.</li>\n<li>While you keep the <strong>&quot;index&quot;</strong> as the last character <em>(textBox<strong>1</strong>, textBox<strong>2</strong>, ..., textBox<strong>N</strong>)</em> you can create a logical order.</li>\n</ol>\n<p>Example in <strong>design mode</strong>:</p>\n<p><a href="https://i.stack.imgur.com/uyCjW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uyCjW.png" alt="Many textboxes in one window" /></a></p>\n<p><strong>Running:</strong></p>\n<p><a href="https://i.stack.imgur.com/atES2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/atES2.png" alt="running example" /></a></p>\n<p>A <strong>similar logic</strong> 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.</p>\n'}]
['https://Stackoverflow.com/questions/39541', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4228/']