Question Dynamic coding?

kaiser

Member
Joined
Dec 11, 2007
Messages
16
Programming Experience
1-3
I am not sure if this is the right spot to ask this question, but I will post it here anyhow...

I know hoe to add a control at runtime to a form, what I would like to know is if there is a way to add code programmatically at runtime as well?

An example would be, when a form opens, a button is added to a form. That is great but a button does not do any good if it does not handle the click event. So I am thinking of something like this:
VB.NET:
Private Sub Form1_Load

Dim button1 As New Button
        button1.Visible = True
        button1.Width = 100
        button1.Height = 100
        button1.Left = 100
        button1.Top = 100
        Controls.Add(button1)

'add code to button1, this is where I do not know what to do?
button1.code = "form2.show()"

End Sub
 
You can dynamically add Event Handlers using

VB.NET:
        AddHandler <OBJECT>.<EVENT>, AddressOf <Sub Name Here>

VB.NET:
Private Sub Form1_Load

Dim button1 As New Button
        button1.Visible = True
        button1.Width = 100
        button1.Height = 100
        button1.Left = 100
        button1.Top = 100
        AddHandler button1.Click, AddressOf ShowForm2
        Controls.Add(button1)
End Sub

private sub ShowForm2(sender as object, e as System.EventArgs)
    form2.show()
end sub
 
Back
Top