ha***********@gmail.com wrote:
I have a form with 104 buttons. How can I create
the same handler like clicking on the button then use the button index
to fetch the element information from the database without writing the
code 104 times!! I could do this easily with VB6 bot not with dot net.
Visual Basic no longer has VB 'Proper's Control Arrays but, to be fair,
it doesn't need them.
Sub AnyButton_Click( sender as Object, e as EventArgs ) _
Handles Button1.Click, Button2.Click, Button3.Click, ...
MsgBox( DirectCast( sender, Button ).Name & " was clicked" )
End Sub
Or, alternatively, use the AddHandler statement in code.
Of course, although we no longer have Control Arrays, we /can/ have
Arrays of Controls:
Dim oButtons as Button() = New Button() _
{ Button1, Button2, Button3 }
For Each eBtn as Button in oButtons
AddHandler eBtn.Click, AddressOf AnyButton_Click
Next
HTH,
Phill W.