473,387 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help on dynamically changing an event handler

I need help dynamically resetting an event handler.

I have a form with several buttons. I have intialized the Click event
of a particular button thus on the form:

_button_3.Click += new
EventHandler(the_controller.onButton3Click_NewEE);

On the same form, I am establishing a property so that I can reset the
Click event from another class (first part of my question: how do I
establish the get accessor since _button_3.Click has to be on the left
side of += or -=?):

public EventHandler Button3Click{
get { ; }
set { _button_3.Click += value; }
}

To dynamically change the event handler, I want to reset it from
another class containing my Click methods but I have to have the get
accessor (in this case, I want to reset the Click even on button 3
when I click on button 5):

public void onButton5Click(Object o, EventArgs e){
if (some condition){
the_form.Button3Click += new EventHandler(this.onButton3Click_A);
}else{
the_form.Button3Click += new EventHandler(this.onButton3Click_B);
}

}

I also need to remove the event handlers, don't I?
Jun 27 '08 #1
4 2869
Yes you do need to remove unwanted event handlers. Do this the same way as
you added it, except replace the "+=" with "-=".
Peter
"bjjnova" <bj*****@gmail.comwrote in message
news:d7**********************************@a23g2000 hsc.googlegroups.com...
>I need help dynamically resetting an event handler.

I have a form with several buttons. I have intialized the Click event
of a particular button thus on the form:

_button_3.Click += new
EventHandler(the_controller.onButton3Click_NewEE);

On the same form, I am establishing a property so that I can reset the
Click event from another class (first part of my question: how do I
establish the get accessor since _button_3.Click has to be on the left
side of += or -=?):

public EventHandler Button3Click{
get { ; }
set { _button_3.Click += value; }
}

To dynamically change the event handler, I want to reset it from
another class containing my Click methods but I have to have the get
accessor (in this case, I want to reset the Click even on button 3
when I click on button 5):

public void onButton5Click(Object o, EventArgs e){
if (some condition){
the_form.Button3Click += new EventHandler(this.onButton3Click_A);
}else{
the_form.Button3Click += new EventHandler(this.onButton3Click_B);
}

}

I also need to remove the event handlers, don't I?
Jun 27 '08 #2
On May 7, 8:16*pm, "Peter Bromberg [C# MVP]"
<pbromb...@nospammaam.yahoo.comwrote:
Yes you do need to remove unwanted event handlers. Do this the same way as
you added it, except replace the "+=" with "-=".
Peter"bjjnova" <bjjn...@gmail.comwrote in message

news:d7**********************************@a23g2000 hsc.googlegroups.com...
I need help dynamically resetting an event handler.
I have a form with several buttons. *I have intialized the Click event
of a particular button thus on the form:
_button_3.Click += new
EventHandler(the_controller.onButton3Click_NewEE);
On the same form, I am establishing a property so that I can reset the
Click event from another class (first part of my question: how do I
establish the get accessor since _button_3.Click has to be on the left
side of += or -=?):
public EventHandler Button3Click{
get { ; }
set { _button_3.Click += value; }
}
To dynamically change the event handler, I want to reset it from
another class containing my Click methods but I have to have the get
accessor (in this case, I want to reset the Click even on button 3
when I click on button 5):
public void onButton5Click(Object o, EventArgs e){
* * * * * *if (some condition){
the_form.Button3Click += new EventHandler(this.onButton3Click_A);
}else{
the_form.Button3Click += new EventHandler(this.onButton3Click_B);
}
}
I also need to remove the event handlers, don't I?- Hide quoted text -

- Show quoted text -
Thanks Peter; can you tell me the answer to my other question? I
cannot write get { return _button_3.Click}: how do I write the get
accessor?
Jun 27 '08 #3
On Wed, 07 May 2008 17:38:47 -0700, bjjnova <bj*****@gmail.comwrote:
Thanks Peter; can you tell me the answer to my other question? I
cannot write get { return _button_3.Click}: how do I write the get
accessor?
You don't. Only the class defining the event has access to the currently
subscribed delegates.

Since "get { return _button_3.Click; }" doesn't make any sense, maybe you
can try to explain more precisely what it is you are trying to do. Note
that as a general rule, code outside a class defining an event has no
access to the subscribers to the event. There would be a serious
encapsulation problem if it were otherwise.

You could write an event in your own class that provides functionality
like clearing existing subscribers, etc. but you can't retrofit an
existing class to do that. Your only option would be to wrap the event
management in something else (like a different event, or methods for the
purpose) and always use that for your subscribing and unsubscribing. Note
that even then, only those delegates you've subscribed yourself via the
wrapper would be subject to your manipulation.

Pete
Jun 27 '08 #4
bjjnova wrote:
I need help dynamically resetting an event handler.

I have a form with several buttons. I have intialized the Click event
of a particular button thus on the form:

_button_3.Click += new
EventHandler(the_controller.onButton3Click_NewEE);

On the same form, I am establishing a property so that I can reset the
Click event from another class (first part of my question: how do I
establish the get accessor since _button_3.Click has to be on the left
side of += or -=?):

public EventHandler Button3Click{
get { ; }
set { _button_3.Click += value; }
}

To dynamically change the event handler, I want to reset it from
another class containing my Click methods but I have to have the get
accessor (in this case, I want to reset the Click even on button 3
when I click on button 5):

public void onButton5Click(Object o, EventArgs e){
if (some condition){
the_form.Button3Click += new EventHandler(this.onButton3Click_A);
}else{
the_form.Button3Click += new EventHandler(this.onButton3Click_B);
}

}

I also need to remove the event handlers, don't I?
Actually not.

One way of solving your problem is:

/* in constructor or InitializeComponent */
button_3.Click += ClickForwarder;

/* in class definition */
public EventHandler Button3Click{ get; set; }
void ClickForwarder(object sender, EventArgs e)
{
if (sender == button_3 && Button3Click != null) Button3Click(sender, e);
}
This way you never need to add/remove event handlers on button 3 itself, you
maintain your own multicast delegate variable containing user-supplied
handlers.
Jun 27 '08 #5

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

Similar topics

27
by: Nicholas Couch | last post by:
I have a little form with a couple of dynamically generated list boxes. When the user makes a selection from the first box, the second box is refreshed. When they make a selection from the second...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
15
by: Amit D.Shinde | last post by:
I am adding a new picturebox control at runtime on the form How can i create click event handler for this control Amit Shinde
0
by: Chad | last post by:
I know that in order to receive events from dynamically added controls, you need to recreate the control with each postback, and then re-attach the event handler. This is thge first time I am...
7
by: Ron Goral | last post by:
Hello I am new to creating objects in javascript, so please no flames about my coding style. =) I am trying to create an object that will represent a "div" element as a menu. I have written...
5
by: J | last post by:
I am having problems dynamically adding more than one event handler to an input. I have tried the Javascript included at the bottom. The lines inp.attachEvent('onkeyup',...
3
by: ICPooreMan | last post by:
The following is a very simple example of what I want to do take an elements oncontextmenu and changing it dynamically onclick of that same element. The code below will fail unless you change the...
4
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.