Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Scenario:
I have designed a Control inheriting from TextBox.
I have implemented an event, which works well (does its job).

Problem:
I would like my Control to do something (invoke a method to check something and set some properties etc) every time this event is assigned from other class (the Form that contains my Control).

I am sure that this is possible.

I tried using add and remove keywords.
However, this broke my code in my OnEventName method.
It seems that an event declared with the add and remove syntax can neither be tested for null nor invoked...

So, what can I do?
I helped lots of people (I have gold authority). Maybe I can be helped by others too.
Thank you.


Some code:
C#
public class MyTextBox : TextBox
{
    /* ... */

    public event MyEventHandler MyEvent;

    protected virtual void OnMyEvent(MyEventArgs e)
    {
        if (MyEvent != null)
            MyEvent(this, e);
    }

    /* ... */
}

public class MyForm : Form
{
    /* ... */

    private MyTextBox myTextBox;

    /* ... */

    public MyForm()
    {
        myTextBox = new MyTextBox();
        this.Controls.Add(myTextBox);
        myTextBox.MyEvent += SomeMethod; // Compatible arguments
        // And now, the Problem!
        // I want myTextBox to "feel" that something was assigned to its event and act accordingly.
    }

    /* ... */
}


The change I tried:
C#
public class MyTextBox : TextBox
{
    /* ... */

    public event MyEventHandler MyEvent
    {
        add { MyEvent += value; DoSomething(); }     // Here I grab the opportunity to react in some way...
        remove { MyEvent -= value; DoSomething(); }  // Same here.
    }

    protected virtual void OnMyEvent(MyEventArgs e)
    {
        if (MyEvent != null)   // Error: The event 'MyEvent' can only appear on the left hand side of += or -=
            MyEvent(this, e);  // The same error.
    }

    /* ... */
}
Posted
Updated 6-Apr-11 9:07am
v3
Comments
[no name] 6-Apr-11 8:27am    
I might be helpful for you to show what you have done so we can point out any problems

You tried something like this?

XML
private EventHandler<EventArgs> onMyEvent;

public event EventHandler<EventArgs> MyEvent
{
add
{
Foo();
onMyEvent = (EventHandler<EventArgs>)Delegate.Combine(onMyEvent, value);
}
remove
{
Bar();
onMyEvent =(EventHandler<EventArgs>)Delegate.Remove(onMyEvent, value);
}
}
 
Share this answer
 
Comments
#realJSOP 6-Apr-11 8:36am    
I gave you a 5 because, even though I've never seen it before, it's still freakin' impressive that a) it's possible, and b) you know about it. :)
Olivier Levrey 6-Apr-11 8:51am    
Yes this is what OP needs. Have a 5. Just don't forget to use your private event. However simpler syntax is possible. Why not just keep += and -= to add and remove a handler? This works just fine and is nicer than this very heavy syntax. In C++/CLI it is also possible to customize the raise method: http://www.codeproject.com/Answers/171085/Can-Event-be-compared-with-nullptr-in-Cplusplus-CL.aspx#answer2
Toli Cuturicu 6-Apr-11 9:00am    
Your point is good.
[no name] 6-Apr-11 9:08am    
Yes could have used the += and -=, but this is the snippet I had on hand.
[no name] 6-Apr-11 8:54am    
I knew the syntax was available, just never found a use for it. Until now :) Thanks
According to these examples on MSDN[^], you are able to test for null etc.
 
Share this answer
 
Comments
Toli Cuturicu 6-Apr-11 8:52am    
Sorry, but it gives an error to me. Please look at the code sample I provided. Thank you.
Henry Minute 6-Apr-11 8:59am    
The example page I linked to used a delegate (in the third one anyway) and tested that for null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900