Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / April 2006

Tip: Looking for answers? Try searching our database.

PLS HELP:Managing events

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
MuZZy - 04 Apr 2006 04:15 GMT
Hi,

I'm developing a usercontrol which has an event SelectedValueChanged.
Below is the simple version of what i have:

//------------------------------
class SelectedValueChangedArgs: EventArgs
{
    object m_oValue;
    public SelectedValueChangedArgs(object oValue)
    {
        m_oValue = oValue;
    }
    public object Value {get{return m_oValue;}}
}
//--
public delegate SelectedValueChangedHandler(object sender,
SelectedValueChangedArgs e);
//--
public MyControl{
    public SelectedValueChangedHandler SelectedValueChanged;
}
//-----------------------------------

The problem is that i have to make sure that any event handler added to
that event is added only once, so i should use it like this:

SelectedValueChangedHandler h = new
SelectedValueChangedHandler(SomehandlerFundction);

myControl.SelectedValueChanged -= h;
myControl.SelectedValueChanged += h;

But the problem is that i can't control all 30 developers in the company
and i ended up spending the whole weekend cleaning up after them (adding
-= to make sure the handler is not added second time somewhere).

So now i'm thinking of making the event protected and creating a
function which will be adding a handler:

// -------- CODE ---------
class MyControl{
    <...>
    public void AddSelectedValueChagedHandler(SelectedValueChangedHandler h)
    {
        SelectedValueChanged -= h;
        SelectedValueChanged +- h;
    }
}
///-----------------------

Is there a standard way/pattern to make my life easier here? :) I think
i've seen somewhere that you can override -=/+- for an event or
something like that. I still would like to go with event instead of a
wrapper function as above.

Any ideas/comments are HIGHLY APPRECIATED!!!

Thank you in advance,
MuZZy
Dmytro Lapshyn [MVP] - 04 Apr 2006 13:35 GMT
Hi,

I recall there is a way to provide a custom implementation of the += and -=
operations for the event. The syntax, as I remember it, is something similar
to how a property is defined:

public event MyEvent
{
 add
 {
   // The += implementation goes here.
 }
 remove
 {
   // The -= implementation goes here.
 }
}

> Hi,
>
[quoted text clipped - 56 lines]
> Thank you in advance,
> MuZZy
MuZZy - 04 Apr 2006 14:38 GMT
Thanks a lot Dmitro! That's exactly what i was looking for! You made my
day :)

> Hi,
>
[quoted text clipped - 75 lines]
>> Thank you in advance,
>> MuZZy

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.