Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>VB.NET does not use control arrays by default. This is because events are handled differently from VB6.</p> <p>If I remember right, this is how you'd write code for a control array, such as an array of <code>CommandButton</code>.</p> <pre><code>Private Sub Buttons_Click(index As Integer) Select Case index Case 1 'write some code here' End Select End Sub </code></pre> <p>The VB6 compiler would then parse the name of each sub in your file and assign them to the appropriate control's event. Thus the subroutine above would attach itself to each button on your form called Buttons and you could determine the button which was clicked by checking the index parameter.</p> <p>In VB.NET, events are 'attached' to controls differently.<br> It is done manually by using the <a href="http://msdn.microsoft.com/en-us/library/ms172919%28v=VS.80%29.aspx" rel="nofollow"><code>AddHandler</code></a> statement or automatically by using the <a href="http://msdn.microsoft.com/en-us/library/6k46st1y%28v=VS.80%29.aspx" rel="nofollow"><code>Handles</code></a> keyword.</p> <p>When you assign events to controls from the Form Designer, the IDE uses the <code>Handles</code> keyword to hook up each of the controls to their event handlers. Thus, you end up with something of this sort:</p> <pre><code>Private Sub Button_Click(sender As Object, e As System.EventArgs) Handles Button.Click 'your code goes here' End Sub </code></pre> <p>With this method, you do not need a control array, as in VB6. You can simply hook up more <strong>Button Clicks</strong> or for that matter, any control's click by adding to the end of the statement like so:</p> <pre><code>Private Sub Button_Click(sender As Object, e As System.EventArgs) Handles _ Button1.Click, Button2.Click, Button3.Click, ListBox.Click 'your code goes here' End Sub </code></pre> <p>As can be seen, you just add the name of the control (e.g. <em>Button2</em>) and then follow it with <code>.Click</code> in order to 'hook' the control's click event to the subroutine.</p> <p>The question then arises, <em>how do you find which control (button) was clicked?</em><br> The answer is quite simple, test the <code>sender</code> parameter.</p> <p>In .NET, typically, the control which is raising an event is always found in the <code>sender</code> parameter. So building on the code above, we'll have something like this:</p> <pre><code>Private Sub Button_Click(sender As Object, e As System.EventArgs) Handles _ Button1.Click, Button2.Click, Button3.Click, ListBox.Click If sender Is Button1 Then 'do something' ElseIf sender Is Button2 Then 'do another thing' ElseIf sender Is Button3 Then 'do yet another thing' Else 'do something different for the ListBox' End If End Sub </code></pre> <p>If you've followed this far and understood it, you'll begin to realise why we do not use Control Arrays in VB.NET or for that matter, .NET in general any more.</p> <p>Notify me if you have any more questions. Cheers!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload