Adding an event dynamically in VS2005

R

Ryan

I have created a form frmData without any controls in it. Then I created a
main form frmMain; added a button btnOpenFormData and added code in button
click event to add radio button acontrol dynamically to frmData as:

Code in frmMain_load event:

frmData = new frmData();
RadioButton rdo = new RadioButton();
rdo.Name = "MyRadiobutton1";
rdo.Text = "My Radio Button";
f.Controls.Add(rdo);
f.show();

When application is launched; frmData is diaplsyed on top of frmMain.

How do I add an event MyRadiobutton1_Click so when radio button on frmData
is clicked then the form will be closed automatically.
 
R

Ryan

Thanks, I was able to accomplish with your aid.

Peter Duniho said:
[...]
How do I add an event MyRadiobutton1_Click so when radio button on
frmData
is clicked then the form will be closed automatically.

I'm a bit confused by your post. You wrote that you "added code in button
click event to add radio button", but you say that the method in question
is named "frmMain_load". That seems odd. Why would you name a Click
event handler with the word "load"? :)

That said...

Assuming you've already written the method "radio1_Click", then just add
to the code you posted:

rdo.Click += radio1_Click;

The method "radio1_Click" will need to have the standard .NET EventHandler
signature:

void radio1_Click(object sender, EventArgs e) { ... }

For what it's worth, the VS Designer doesn't do anything magical. You can
look at the *.Designer.cs files it generates for inspiration regarding
hand-coding UI implementations. For example, you've already gotten it to
hook up a Click event handler (or is it Load?); you could look at the
frmMain.Designer.cs file to see how VS did it, and then use that as a
guide (at the very least, it would help you know where in the MSDN docs to
look for a more detailed explanation).

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top