Results 1 to 7 of 7

Thread: Determining which programatically created Button has been pressed

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2008
    Posts
    28

    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

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Determining which programatically created Button has been pressed

    Try:
    Code:
    Dim btn As Button = DirectCast(sender, Button)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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)

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2008
    Posts
    28

    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.")

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: Determining which programatically created Button has been pressed

    Quote Originally Posted by vodkasoda View Post
    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.")
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2008
    Posts
    28

    Re: Determining which programatically created Button has been pressed

    Quote Originally Posted by dday9 View Post
    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 ...

Tags for this Thread

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