views:

23

answers:

2

hi there :) i just want to ask on how to make an array of buttons in vb .net 2005. It seems very different in vb6 'coz in vb6 you have just to add an index on its property but in vb.net there is no index property. Can it be made by designing in on the form just like copying and pasting on the form in vb6?

+1  A: 

Unlike in VB6, .NET allows you to create normal arrays of controls so there’s no need for the control collections of VB6.

Just use an array or a List(Of Control), fill it with controls and presto!, you’re done.

Use AddHandler if you want to add event handlers to these controls.

The Windows Forms designer no longer supports control arrays (in contrast to VB6’ form designer) – which makes sense: If you’ve got a control array, this is inherently dynamic so it cannot be meaningfully created at design time. Its size grows and shrinks dynamically at run-time depending on the actual number of controls that are needed.

Konrad Rudolph
Additionally, there is a collection of controls within the form (me.Controls) which has a hierarchical list of all the controls on the current form.
Wade73
A: 

VB.NET does not use control arrays by default. This is because events are handled differently from VB6.

If I remember right, this is how you'd write code for a control array, such as an array of CommandButton.

Private Sub Buttons_Click(index As Integer)
    Select Case index
        Case 1
            'write some code here'
    End Select
End Sub

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.

In VB.NET, events are 'attached' to controls differently.
It is done manually by using the AddHandler statement or automatically by using the Handles keyword.

When you assign events to controls from the Form Designer, the IDE uses the Handles keyword to hook up each of the controls to their event handlers. Thus, you end up with something of this sort:

Private Sub Button_Click(sender As Object, e As System.EventArgs) Handles Button.Click
    'your code goes here'
End Sub

With this method, you do not need a control array, as in VB6. You can simply hook up more Button Clicks or for that matter, any control's click by adding to the end of the statement like so:

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

As can be seen, you just add the name of the control (e.g. Button2) and then follow it with .Click in order to 'hook' the control's click event to the subroutine.

The question then arises, how do you find which control (button) was clicked?
The answer is quite simple, test the sender parameter.

In .NET, typically, the control which is raising an event is always found in the sender parameter. So building on the code above, we'll have something like this:

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

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.

Notify me if you have any more questions. Cheers!

Alex Essilfie
wow! my problem has been answered :) thanks to you guys..
johntotetwoo
@hohntotetwoo: Great to have been of help to you. Now go on and accept an answer (click on the check mark next to an answer). Otherwise vote for both answers.
Alex Essilfie