Determining which programatically created Button has been pressed
I think I've confused myself with this code !!!
I am creating Buttons down my Form that are incrementally numbered using the following code.
At the moment this is all I have, and the MessageBox will be replaced by real code, but the display always gives the last Button created, obviously, how can I ascertain which Button was pressed ?!?
Code:
Private Sub Main_Screen()
myRow = 0
myPosition = 87
While myDr.Read()
myRow = myRow + 1
myBtn = "ProcessButton_" & myRow & "_1"
myPosition = myPosition + 30
myPoint = New Point(12, myPosition)
myWidth = 68
myHeight = 26
myName = "Process"
CreateButton(myBtn)
End While
myDr.Close()
mySQLcmd.Dispose()
myConn.Close()
End Sub
Sub CreateButton(myButtonName)
Dim myBtn As New Button
myBtn.Location = myPoint
myBtn.Width = myWidth
myBtn.Height = myHeight
myBtn.Text = myName
myBtn.Name = myButtonName
AddHandler myBtn.Click, AddressOf myBtn_Click
Controls.Add(myBtn)
End Sub
Private Sub myBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show(myBtn & " is clicked.")
End Sub
Re: Determining which programatically created Button has been pressed
Why do you think that MS went to all the trouble of providing every event handler with a sender variable?
Re: Determining which programatically created Button has been pressed
Try:
Code:
Dim btn As Button = DirectCast(sender, Button)
Re: Determining which programatically created Button has been pressed
Another option is to use a DataGrid with a ButtonColumn, that gives you details about the row that is clicked (although I forget exactly how to set it up, it's a bit funky, you need to do something weird on the binding of each row to wire things up correctly.)
The other option is to associate a different sub with each button. No, I don't mean writing a sub for each button, but you can write a function that will take an integer and return a Sub (untested, don't have VB to hand):
Function GetButtonClickHandler(rowNumber As Integer) As Action(Of Object, EventArgs)
Return Sub (sender, args) MessageBox.Show("Button " & rowNumber & " is clicked.")
End Function
' ... in your Create Button ...
AddHandler myBtn.Click, GetButtonClickHandler(myRow)
Re: Determining which programatically created Button has been pressed
Thanks for your replies, in order :
@dunfiddlin ... I am very new to VB.net programming and I am afraid I don't know what a sender variable is ...
@dday9 ... Is that as an extra variable or in place of one of the others ?
@Evil_Giraffe ... I decided against a DataGridView because (a) it's ugly & (b) the data I am putting on the Form comes from more than one SQL Table & a DataGridView doesn't like that. A DataGrid is the VB6 version, is it not, and as I am upgrading from VBA to VB.Net I don't really want to use retro-options ... maybe I am wrong ?!? I will look at your Function option tomorrow, thanks.
However ... this has just been supplied to me from elsewhere, which works ...
Code:
MessageBox.Show(CType(sender, Button).Name & " is clicked.")
Re: Determining which programatically created Button has been pressed
Quote:
Originally Posted by
vodkasoda
Thanks for your replies, in order :
@dunfiddlin ... I am very new to VB.net programming and I am afraid I don't know what a sender variable is ...
@dday9 ... Is that as an extra variable or in place of one of the others ?
@Evil_Giraffe ... I decided against a DataGridView because (a) it's ugly & (b) the data I am putting on the Form comes from more than one SQL Table & a DataGridView doesn't like that. A DataGrid is the VB6 version, is it not, and as I am upgrading from VBA to VB.Net I don't really want to use retro-options ... maybe I am wrong ?!? I will look at your Function option tomorrow, thanks.
However ... this has just been supplied to me from elsewhere, which works ...
Code:
MessageBox.Show(CType(sender, Button).Name & " is clicked.")
I'm declaring a button variable and setting it to whichever button the click event is being fired from. So if you wanted to get the name from the button the way I showed it would look like this:
Code:
Dim btn As Button = DirectCast(sender, Button)
MessageBox.Show(btn.Name & " is clicked.")
Re: Determining which programatically created Button has been pressed
Quote:
Originally Posted by
dday9
I'm declaring a button variable and setting it to whichever button the click event is being fired from. So if you wanted to get the name from the button the way I showed it would look like this:
Code:
Dim btn As Button = DirectCast(sender, Button)
MessageBox.Show(btn.Name & " is clicked.")
OK, I see, yes, thank you ...