Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

How to determine which programatically created Button has been pressed

$
0
0

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 ?!?

    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


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>