[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.
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
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
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!
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.