borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (vr toolbox and vb .net)Next Topic (JavaScript in vb .net) New Topic New Poll Post Reply
AndreaVB Forum : VB.Net : Control Array in VB.net
Poster Message
Chris_871
Level: Master


Registered: 30-11-2002
Posts: 106

icon Control Array in VB.net

Dear Friends,

VB.Net is not supporting Control Array. But it can be done through some other way.
In .Net a single Button click event can handle the multiple click event for other objects. For that you need to list the events that need to be handled after the
Handles keyword.

Note : The parameter list of the handler must match the parameters defined for the event.

Consider the following example.

In this example, after the handles keyword of button1_click event i have included all other button click events.
when I click any one of the button, it handles the same code.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click

        Select Case sender.name
            Case "Button1"
                MsgBox("You have selected button1")
            Case "Button2"
                MsgBox("You have selected button2")
            Case "Button3"
                MsgBox("You have selected button3")
            Case "Button4"
                MsgBox("You have selected button4")
            Case "Button5"
                MsgBox("You have selected button5")
        End Select
    End Sub


Try this example.

20-11-2003 at 05:43 PM
View Profile Send Email to User Show All Posts | Quote Reply
dynamic sysop
Level: Guest

icon Re: Control Array in VB.net

something to point out , if you switch " Option Strict " to " ON " then you will get an error when trying to use " sender.name " , you should cast the object sender as a Button.
also you can handle all the clicks in one sub without a long list of " Handles button1_click , button2_click " etc..., here's a simple example of how to handle all buttons on a form / cast the sender...

    Private WithEvents btn As Button

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ctl As Control
        For Each ctl In Me.Controls
            If TypeOf ctl Is Button Then
                AddHandler ctl.Click, AddressOf btn_Click
            End If
        Next
    End Sub

    Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
        Dim butn As Button = DirectCast(sender, Button)
        MessageBox.Show("you just clicked... " & butn.Name)
    End Sub

24-11-2003 at 03:45 PM
| Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Control Array in VB.net

As a footnote, Option Strict is by default Off (to those who are begining to learn VB.NET). But, if you turn it On, Chris_871 code will still work with small change:

MsgBox("You have selected " & CType(Sender,Button).Name)

instead of Select Case .... End Select

Altough, dynamic sysop's code is a good example of using AddHandler method.

____________________________
If you find the answer helpful, please mark this topic as solved.

26-11-2003 at 09:41 PM
View Profile Send Email to User Show All Posts | Quote Reply
Goran
Level: Moderator

Registered: 16-05-2002
Posts: 1681
icon Re: Control Array in VB.net

As a footnote, Option Strict is by default Off (to those who are begining to learn VB.NET). But, if you turn it On, Chris_871 code will still work with small change:

MsgBox("You have selected " & CType(Sender,Button).Name)


instead of Select Case .... End Select

Altough, dynamic sysop's code is a good example of using AddHandler method.

____________________________
If you find the answer helpful, please mark this topic as solved.

26-11-2003 at 09:42 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB.Net : Control Array in VB.net
Previous Topic (vr toolbox and vb .net)Next Topic (JavaScript in vb .net) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder