Robby
Ultimate Contributor
I'm bored and fooling around, anyway....
There have been a few questions today about creating controls at runtime, hence the fooling around. (Did I mention I was bored)
The following code creates 3 buttons, but is still hard-coded, there must be a way of making this more dynamic.
Any ideas?
There have been a few questions today about creating controls at runtime, hence the fooling around. (Did I mention I was bored)
The following code creates 3 buttons, but is still hard-coded, there must be a way of making this more dynamic.
Any ideas?
Visual Basic:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim btn1 As New Button()
Dim btn2 As New Button()
Dim btn3 As New Button()
With btn1
AddHandler .Click, AddressOf btnClick
.Text = "button 1"
.Location = New Point(0, 50)
.Size = New Size(100, 50)
End With
With btn2
AddHandler .Click, AddressOf btnClick
.Text = "button 2"
.Location = New Point(0, 150)
.Size = New Size(100, 50)
End With
With btn3
AddHandler .Click, AddressOf btnClick
.Text = "button 3"
.Location = New Point(0, 250)
.Size = New Size(100, 50)
End With
Controls.Add(btn1)
Controls.Add(btn2)
Controls.Add(btn3)
End Sub
Private Sub btnClick(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("You clicked ... " & DirectCast(DirectCast(sender, Button).Text, String))
End Sub