Dynamic button creation in VB.NET with click events
It is simple to create a button runtime. But it is one more step to define a respective event handler for the events for the control.
Load buttons
Private Sub loadButtons()Dim intNoOfTextBox As Integer = 10For intC As Integer = 0 To intNoOfTextBox - 1Dim objButton As ButtonobjButton = New ButtonobjButton.Size = New Size(200, 25)objButton.Location = New Point(5, intC * 25)objButton.Text = " My Runtime Dynamic Button " & (intC + 1).ToStringAddHandler objButton.Click, AddressOf ButtonClickMe.Controls.Add(objButton)NextEnd Sub
Event handler for button click
Private Sub ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim objTempButton As Button = CType(sender, Button)
Dim strButtonText As String = ""
strButtonText = objTempButton.Text.Trim
MessageBox.Show(strButtonText)
End Sub
No comments:
Post a Comment