One feller's views on the state of everyday computer science & its application (and now, OTHER STUFF) who isn't rich enough to shell out for www.myfreakinfirst-andlast-name.com
Using 89% of the same design the blog had in 2001.
FOR ENTERTAINMENT PURPOSES ONLY!!!
Back-up your data and, when you bike,
always wear white.
As an Amazon Associate, I earn from qualifying purchases. Affiliate links in green.
Thursday, February 19, 2004
What? You want to programmatically add a button to a VB.NET Windows.Forms Form/Panel/User Control and hook up an event handler? Heck, that's easy. Here we go. Start a new WindowsApplication and open up Form1 to the code view.
Paste in this:
Friend WithEvents cmdTemp As _
System.Windows.Forms.Button = New Button
Private Sub cmdTemp_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdTemp.Click
MsgBox("I've been clickified!!!")
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(Me.cmdTemp)
End Sub
Enjoy. For more form designerless goodness, make your canvas a PnlGridLayout. For ugly, generic UIs where you don't know what's coming ahead of time, using the Java-style GridLayout and adding things to the Form.Controls collection really is the way to go.
Even neater is when you hook up handlers with the AddHandler function, like this:
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(Me.cmdTemp)
AddHandler cmdTemp.Click, AddressOf Me.genericButtonHandler
End Sub
Private Sub genericButtonHandler(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
MsgBox("send all the button clicks you want here!")
End Sub
You can use the AddHandler jive to hook that sub up to any button you make at any time, which is awfully useful stuff when creating buttons out of thin air.
posted by ruffin
at 2/19/2004 02:13:00 PM
The postings on this site are [usually] my own and do not necessarily reflect the views of any employer, past or present, or other entity. About Our Author