Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Determining which button was pressed

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    263

    Resolved [RESOLVED] [2005] Determining which button was pressed

    How do I reference a runtime-created button that was pressed?

    I've created five buttons with the following code:

    Code:
    For i As Integer = 0 To 4
                Dim PnlNavigatorButton As New System.Windows.Forms.Button
                Dim ButtonNames() As String = {"Search", "Supervising", "Shipping", "Production", "Data Entry"}
    
                With PnlNavigatorButton
                    .Visible = True
                    .Location = New System.Drawing.Point(15, 75 + x)
                    .Size = New Size(96, 32)
                    .Text = ButtonNames(i)
                End With
    
                pnlTabNavigator.Controls.Add(PnlNavigatorButton)
                x += 32
    
                AddHandler PnlNavigatorButton.Click, AddressOf PnlNavigatorButton_Click
    
            Next
    Now I'm working with the PnlNavigatorButton_Click procedure. I imagine that it would look something like:

    Code:
    Dim control As Control
            For Each control In pnlTabNavigator.Controls
                 If control = 'Here's what I'm Missing
    
            Next
    I just don't know how to reference the system event of a button click to perform the if-then test. I know I've seen it somewhere, somewhen, but my dog ate my map back there... I keep thinking it's something like "e.buttonClicked" but none of that shows up in intellisense.

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2005] Determining which button was pressed

    No, if you look at your PnlNavigatorButton_Click function, see that it takes "sender" as an argument. That is a direct reference to which button was pushed.

    Code:
        Private Sub PnlNavigatorButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
            MsgBox(DirectCast(sender, Button).Name)
    
        End Sub
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2005] Determining which button was pressed

    Inside your method, cast the sender object a button, then use its text to identify it.

    Code:
    Private Sub PnlNavigatorButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Select Case DirectCast(sender, Button).Name
            Case "Search"
    'search code
            Case "Supervising"
    'supervising code
            Case "Shipping"
    'shipping code
            Case "Production"
    'production code
            Case "Data Entry"
    'data entry code
        End Select
    End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    263

    Re: [2005] Determining which button was pressed

    Laugh, thank you! I'm still finding my footing in this language and I tend to miss obvious things that result from me not knowing the nature of the objects I'm working with. But thank you!

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    263

    Re: [RESOLVED] [2005] Determining which button was pressed

    Just for your info, wild bill, the code you sent didn't quite work, but with a simple change, it did.

    You'd sent:

    Code:
    Select Case DirectCast(sender, Button).Name
    But if I put a messagebox.show in one of the case-selects, it never showed. So I changed the Select Case statement to this:

    Code:
    Select Case sender.text.ToString
    And it worked! Still, thank you.

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