Results 1 to 4 of 4

Thread: How to capture Button Index in .NET

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    28

    Angry How to capture Button Index in .NET

    Dear All,
    In VB6 we can simply find out the command button index if we have series of buttons on form.

    Example VB6 Code:-
    Sub Command1_Click(Index as Integer)
    ' msgbox "You are pressed Button number :" & index
    End Sub

    How do we do it VB .NET ? so that we know what button we are pressed !

    my .NET sample code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click

    ? What is the command to capture the button index

    End Sub

    TIA

    regards
    Jas

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    It doesn't work that way in .Net, but to answer your question, the sender object that is passed in as an argument contains which button was clicked. Try something like this:


    'Might be CommandButton, I am doing this from memory.
    Dim myButton As Button
    myButton = CType(sender, Button)
    MessageBox.Show(myButton.Name.ToString())



    Remember, you have to cut me some slack, I am doing this completely from memory and did not verify it completely in VS.Net. Some changes will probably be needed.

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Assuming you have two buttons :

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
    2.         If sender Is Me.Button1 Then
    3.             MessageBox.Show("You clicked button1")
    4.         Else
    5.             MessageBox.Show("You clicked button2")
    6.         End If
    7.  
    8.     End Sub

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    28

    (Resolved) How to capture Button Index in .NET

    Hi Guys,
    Thanks for the advise and help i manage to complete my assignment now

    Thanks a lot

    regards
    Jas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width