473,385 Members | 1,875 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,385 software developers and data experts.

asking about event

hello, i'm still new to vs

i have a question,

how can i clear my event like to set that to null ?
i would like to do something like this without knowing
what is the event handler before
ex: (error)
button1.Click = null;

i know i can clear that manually using such as
button1.Click -= new EventHandler(button1_Click);

but in case there is many event assigned to button1.Click
i think it would be not effecient writing one by one line removing the event
handler
is there another way?

thanks in advance.
Mar 5 '06 #1
3 1206
vj
Hi Kenichi,

I was faced with a similar situation before, I was advised to use Reflection
to find out attached events to object, then you can examine this list and
remove them appropriately. If you are new, the reflection concept can be a
little overwhelming, but if you if have time on hand, I would say please
latch on to it. It is one concept that will help you a lot... Here is
sample..

http://www.vb-helper.com/howto_net_list_events.html

Ok ok.. this is c-sharp group, please excuse me for postin a vb sample..

VJ

"Kenichi" <no****@nospam.com> wrote in message
news:Oo***************@tk2msftngp13.phx.gbl...
hello, i'm still new to vs

i have a question,

how can i clear my event like to set that to null ?
i would like to do something like this without knowing
what is the event handler before
ex: (error)
button1.Click = null;

i know i can clear that manually using such as
button1.Click -= new EventHandler(button1_Click);

but in case there is many event assigned to button1.Click
i think it would be not effecient writing one by one line removing the
event handler
is there another way?

thanks in advance.

Mar 5 '06 #2

"Kenichi" <no****@nospam.com> wrote in message
news:Oo***************@tk2msftngp13.phx.gbl...
hello, i'm still new to vs

i have a question,

how can i clear my event like to set that to null ?
MyEvent = null;
i would like to do something like this without knowing
what is the event handler before
ex: (error)
button1.Click = null;
But this isn't your event it is Button's event (A member of Button COULD
write "Click = null").

The whole point of using event rather than a naked delegate is that it
restricts what can be done to it from outside the declaring class.

i know i can clear that manually using such as
button1.Click -= new EventHandler(button1_Click);

but in case there is many event assigned to button1.Click
i think it would be not effecient writing one by one line removing the
event handler
is there another way?


I cannot imagine a scenario in which the efficiency of adding or removing
event handlers from a button click event is a real issue.

It also seems unlikely to me that multiple handlers on the event is a good
design decision. I suspect that your best approach would be to have a single
handler that calls multiple methods.
Mar 5 '06 #3
Kenichi <no****@nospam.com> wrote:
hello, i'm still new to vs

i have a question,

how can i clear my event like to set that to null ?
You can't clear an event in another object - events themselves *only*
expose ways of subscribing and unsubscribing.
i would like to do something like this without knowing
what is the event handler before
ex: (error)
button1.Click = null;

i know i can clear that manually using such as
button1.Click -= new EventHandler(button1_Click);

but in case there is many event assigned to button1.Click
i think it would be not effecient writing one by one line removing the event
handler is there another way?


Yes, that would probably be more efficient - very, very slightly, in a
way which would be insignificant to virtually all programs. However, it
would break the encapsulation of events.

If you're writing your own class which exposes an event, of course, you
could include a public method of ClearFooEventHandlers which set the
underlying delegate variable to null, if you want.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 5 '06 #4

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

Similar topics

24
by: deko | last post by:
I have an mdb (2002 file format) that uses memo fields extensively. I've read a lot about how problematic memo fields can be and that avoiding them is a good idea. But I'm stuck with them and was...
258
by: Terry Andersen | last post by:
If I have: struct one_{ unsigned int one_1; unsigned short one_2; unsigned short one_3; }; struct two_{ unsigned int two_1;
6
by: Deniz Bahar | last post by:
Hi, My english is not top notch and I am trying to understand what K&R2 Ex 5-11 is asking for (pg 118). It says: Modify the programs entab and detab (which I completed from chapter 1 and have...
1
by: Gidi | last post by:
i want the enter key press wll be an event this is my eventhandler header this.dataGrid1.KeyPress+=new System.Windows.Forms.KeyPressEventHandler(this.dataGrid1_KeyPress) is it ok and how do i...
1
by: Zach | last post by:
Re the example I code below. I have tried to make a very consise example of events / eventshandling; would you please comment? (Don't tell me to read a book.) Thanks, Zach.
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
9
by: jeff | last post by:
New VB user...developer... Situation...simplified... - I want to wrap a pre and post event around a system generated where the pre-event will always execute before the system event and the...
1
by: AparnaKulkarni | last post by:
Want to call a crystal report of version 8.5 through VB.NET. In this the data is passed to report through XML file. On running this, the report asks for Database name, Server name, Userid, Pwd. ...
4
by: ITCraze | last post by:
Hello everyone, I have a Gridview , whose first column is template colums which contains checkboxes , outside my GridView I a have a delete button. So in this application,when the user check...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.