By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,686 Members | 1,558 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,686 IT Pros & Developers. It's quick & easy.

How do I add an event to code?

P: n/a
If I have a control, for example a CheckedListBox, how do I add and event
to code, for example that a box has been checked by the user?

Thanks
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
The easiest way is to let VS do most of the work. You can simply
double-click on the control to add an event handler for the control's
default event to your code. For other events, click on the control in the
designer. Next, click on the event icon (the flash of lightning) in the
properties window to display the pre-defined events for this control.
Double-click on the name of the desired event to have VS add an event
handler to your code.

VS writes all necessary code (attaching the event handler to the event,
etc.) behind the scenes. You can view this code by expanding the region
'Windows Form Designer generated dode.' All you'll have to do is write the
code you want the event handler to execute. Of course this only works for
pre-defined events. If you want to create your own event, you'll still have
to go through the usual steps of creating a delegate object, defining and
publishing the event, and attaching the event handler(s).

Hope this helps.

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
"vbMark" <no@email.com> wrote in message
news:Xn************************@130.133.1.4...
If I have a control, for example a CheckedListBox, how do I add and event
to code, for example that a box has been checked by the user?

Thanks

Nov 16 '05 #2

P: n/a
"Kai Brinkmann [MSFT]" <ka******@online.microsoft.com> wrote in
news:uj**************@TK2MSFTNGP09.phx.gbl:
The easiest way is to let VS do most of the work. You can simply
double-click on the control to add an event handler for the control's
default event to your code. For other events, click on the control in
the designer. Next, click on the event icon (the flash of lightning)
in the properties window to display the pre-defined events for this
control. Double-click on the name of the desired event to have VS add
an event handler to your code.

VS writes all necessary code (attaching the event handler to the
event, etc.) behind the scenes. You can view this code by expanding
the region 'Windows Form Designer generated dode.' All you'll have to
do is write the code you want the event handler to execute. Of course
this only works for pre-defined events. If you want to create your own
event, you'll still have to go through the usual steps of creating a
delegate object, defining and publishing the event, and attaching the
event handler(s).

Hope this helps.

Wow! Excellent help. Thanks!
Nov 16 '05 #3

P: n/a
"Kai Brinkmann [MSFT]" <ka******@online.microsoft.com> wrote in
news:uj**************@TK2MSFTNGP09.phx.gbl:
The easiest way is to let VS do most of the work. You can simply
double-click on the control to add an event handler for the control's
default event to your code. For other events, click on the control in
the designer. Next, click on the event icon (the flash of lightning)
in the properties window to display the pre-defined events for this
control. Double-click on the name of the desired event to have VS add
an event handler to your code.

VS writes all necessary code (attaching the event handler to the
event, etc.) behind the scenes. You can view this code by expanding
the region 'Windows Form Designer generated dode.' All you'll have to
do is write the code you want the event handler to execute. Of course
this only works for pre-defined events. If you want to create your own
event, you'll still have to go through the usual steps of creating a
delegate object, defining and publishing the event, and attaching the
event handler(s).

Hope this helps.


If I've added an event that I don't want what's the best way to delete it?
Nov 16 '05 #4

P: n/a
Selecting you code and pressing the delete key.

Telmo Sampaio

"vbMark" <no@email.com> wrote in message
news:Xn************************@130.133.1.4...
"Kai Brinkmann [MSFT]" <ka******@online.microsoft.com> wrote in
news:uj**************@TK2MSFTNGP09.phx.gbl:
The easiest way is to let VS do most of the work. You can simply
double-click on the control to add an event handler for the control's
default event to your code. For other events, click on the control in
the designer. Next, click on the event icon (the flash of lightning)
in the properties window to display the pre-defined events for this
control. Double-click on the name of the desired event to have VS add
an event handler to your code.

VS writes all necessary code (attaching the event handler to the
event, etc.) behind the scenes. You can view this code by expanding
the region 'Windows Form Designer generated dode.' All you'll have to
do is write the code you want the event handler to execute. Of course
this only works for pre-defined events. If you want to create your own
event, you'll still have to go through the usual steps of creating a
delegate object, defining and publishing the event, and attaching the
event handler(s).

Hope this helps.


If I've added an event that I don't want what's the best way to delete it?

Nov 16 '05 #5

P: n/a
Just deleting the event handler isn't enough. You also need to remove the
line of code that registers the event handler with the event. For example,
to remove the Load event for a form called 'MyForm' you need to delete two
things:

1. private void frmMyForm_Load(object sender, System.EventArgs e) { // some
code } (the event handler)
2. this.Load += new System.EventHandler(this.frmMyForm_Load);

The former is the event handler itself, the latter is the line of code that
attaches this event handler to the Load event. You'll find this line in the
*Windows Forms Designer generated code* region.

There is an easier way to do this, though. Go back to the control's
properties and open the events, just like you did to add the event. Select
the name of the event handler next to the event in the list. Delete this
method from the event list. This will remove the code. If there was no code
in the event handler, the handler itself will also be deleted from your
code. If you already wrote code, you still have to delete this method
manually, but all other code related to this event is removed automatically
by VS.

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
"vbMark" <no@email.com> wrote in message
news:Xn************************@130.133.1.4...
"Kai Brinkmann [MSFT]" <ka******@online.microsoft.com> wrote in
news:uj**************@TK2MSFTNGP09.phx.gbl:
The easiest way is to let VS do most of the work. You can simply
double-click on the control to add an event handler for the control's
default event to your code. For other events, click on the control in
the designer. Next, click on the event icon (the flash of lightning)
in the properties window to display the pre-defined events for this
control. Double-click on the name of the desired event to have VS add
an event handler to your code.

VS writes all necessary code (attaching the event handler to the
event, etc.) behind the scenes. You can view this code by expanding
the region 'Windows Form Designer generated dode.' All you'll have to
do is write the code you want the event handler to execute. Of course
this only works for pre-defined events. If you want to create your own
event, you'll still have to go through the usual steps of creating a
delegate object, defining and publishing the event, and attaching the
event handler(s).

Hope this helps.


If I've added an event that I don't want what's the best way to delete it?

Nov 16 '05 #6

P: n/a
oops... this is the C# group... stupid me :)
I will be more careful on the next posts.

Telmo Sampaio

"Kai Brinkmann [MSFT]" <ka******@online.microsoft.com> wrote in message
news:O8**************@TK2MSFTNGP09.phx.gbl...
Just deleting the event handler isn't enough. You also need to remove the
line of code that registers the event handler with the event. For example,
to remove the Load event for a form called 'MyForm' you need to delete two
things:

1. private void frmMyForm_Load(object sender, System.EventArgs e) { // some code } (the event handler)
2. this.Load += new System.EventHandler(this.frmMyForm_Load);

The former is the event handler itself, the latter is the line of code that attaches this event handler to the Load event. You'll find this line in the *Windows Forms Designer generated code* region.

There is an easier way to do this, though. Go back to the control's
properties and open the events, just like you did to add the event. Select
the name of the event handler next to the event in the list. Delete this
method from the event list. This will remove the code. If there was no code in the event handler, the handler itself will also be deleted from your
code. If you already wrote code, you still have to delete this method
manually, but all other code related to this event is removed automatically by VS.

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights. "vbMark" <no@email.com> wrote in message
news:Xn************************@130.133.1.4...
"Kai Brinkmann [MSFT]" <ka******@online.microsoft.com> wrote in
news:uj**************@TK2MSFTNGP09.phx.gbl:
The easiest way is to let VS do most of the work. You can simply
double-click on the control to add an event handler for the control's
default event to your code. For other events, click on the control in
the designer. Next, click on the event icon (the flash of lightning)
in the properties window to display the pre-defined events for this
control. Double-click on the name of the desired event to have VS add
an event handler to your code.

VS writes all necessary code (attaching the event handler to the
event, etc.) behind the scenes. You can view this code by expanding
the region 'Windows Form Designer generated dode.' All you'll have to
do is write the code you want the event handler to execute. Of course
this only works for pre-defined events. If you want to create your own
event, you'll still have to go through the usual steps of creating a
delegate object, defining and publishing the event, and attaching the
event handler(s).

Hope this helps.

If I've added an event that I don't want what's the best way to delete

it?

Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.