it took me ages to find it. i googled "add controls at runtime" and not one single result gave me the intstructions on how to add code to the controls. kinda useless with no code, ehh? all results only told me how to add the control, that's it. well, this result is different. i will teach you firstly how to add controls at runtime, then how to apply code to them at runtime
firstly, add the controls:
create a new windows forms application
then double click on the form and type this:
Dim mybutton As New Button mybutton.Name = "button1" mybutton.Location = New Point(20, 20) mybutton.text = "click me" Me.Controls.Add(mybutton) With mybutton AddHandler .Click, AddressOf button1_click End With
now run it. do you have a button on the form? if yes, excellent. if not, you made a mistake. try again
now how to add code? you may have noticed that when you double click on any control, it says private sub button1_click(blah, blah, blah) Handles button1.click
where it says handles button1.click is what determines when the code executes under what event for what control (in this case the click event for the button1). this won't work when adding code at runtime.
did you notice in the above example i added a with event, and addHandler? this is what creates the handler (same as Handles button1.click)
now we add the code for the runtime control
private sub button1_click() MsgBox("you have added code and a control at runtime!") end sub
above, the addressOf button1_click tells the program to execute the sub button1_click when the button is clicked on (as determined by .click)
but i know what you're thinking! your thinking "why would we even BOTHER doing this at runtime? isn't it easier to do it at design-time in the first place?"
yes it is, providing your not writing a plugin. plugins are code based, not GUI (also reffered to as WYSIWYG). to update existing forms using plugins, you need to use this method.