473,473 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Adding an event dynamically in VS2005

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.
Sep 5 '08 #1
2 1368
On Fri, 05 Sep 2008 15:50:01 -0700, Ryan <Ry**@discussions.microsoft.com>
wrote:
[...]
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
Sep 5 '08 #2
Thanks, I was able to accomplish with your aid.

"Peter Duniho" wrote:
On Fri, 05 Sep 2008 15:50:01 -0700, Ryan <Ry**@discussions.microsoft.com>
wrote:
[...]
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
Sep 10 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
20
by: David | last post by:
I have a one-line script to add an onunload event handler to the body of the document. The script is as follows: document.getElementsByTagName("BODY").onunload=function s() {alert("s")} Now...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
3
by: Ankit Aneja | last post by:
I have a strange situation and I have no idea how to solve this. Its a Recruitment Search Page,in the Admin Page, for every button click event the Admin Person has to create a checkbox on the users...
2
by: Amelyan | last post by:
I *finally* narrowed down my problem to my custom web control. If I hookup event inside dynamically created custom web control, it doesn't get fired. However, event gets fired for any other...
5
by: Mark Rae | last post by:
Hi, Is it possible to add events, specifically a Click event, to a dynamically created TableCell? I have an <asp:Table...> control to which I'm dynamically adding TableRow controls made up of...
11
by: Neo Geshel | last post by:
Greetings. Hopefully someone will be able to untie this Gordian Knot I have found myself bound up in. I am trying to add a submit button dynamically to a PlaceHolder. This button will help...
3
by: bill | last post by:
I am using VS2005 to build a web form dynamically. I'm using AddHandler to connect a custom event handler to the TextChanged event of dynamically added textbox controls. Data entered in the...
4
by: Lewis Holmes | last post by:
Hi I have the following situation in one of my asp.net pages. The user can add multiple table rows to a form by selecting a button. These rows can contain asp.net controls. When this button is...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.