Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Event Hell

Status
Not open for further replies.

transparent

Programmer
Sep 15, 2001
333
GB
I have the following code that SHOULD throw an event 'Cancel' when the btnCancel_Click method runs. The btnCancel_Click method does run however, when OnCancel is called Cancel is always Null!! therefore the line in bold if(Cancel !=null) doesn't throw the event. What am I doing wrong??

public delegate void EventHandler(object sender,EventArgs e);
public event EventHandler Cancel;

private void btnCancel_Click(object sender,System.EventArgs e)
{
//Throw Cancel Event!
OnCancel(new EventArgs());
}

protected virtual void OnCancel(EventArgs e)
{
if(Cancel != null)
{

Cancel(this,e);

}


}
 
transparent,
The Cancel event is always null because no client code has registered to listen to to it. Events are usually raised from inside a class and responded to in a another class. When something happens inside the class, the class itself raises the event. If another class (called the client) wants to respond to an event raised by the first class, such class must register to listen to that event, using the following syntax:
Code:
YourClass.Cancel += 
   [COLOR=blue]new[/color] EventHandler(EventHandlerMethod)
When no client class has registered to listen to an event, checking for the nullability of such event always returns null. In your case, no client class has registered to listen to the Cancel event. In fact, it looks like you're trying to catch the event within the same class. That doesn't make a lot of sense, as what you would normally do is catch the event raised by one class in another class. For example, the click event of the Button class is usually caught in a method of the form where an instance of the button is.

I'm not sure what you're trying to do but try to create a separate class where the Cancel event is raised when something happens. Then, respond to that event from another class.

Hope this helps!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
So how do I throw the cancel event within the btnCancel_Click method? The cancel event would be caught by another usercontrols class.
 
Transparent,
In the class where the event will be caught, declare a variable of the class that throws the event, and register a method with the event delegate. Assuming the class where the button control is is called EventClass, it would look something like this:
Code:
[COLOR=blue]public class[/color] YourUserControlClass
{
  EventClass ev = [COLOR=blue]new[/color] EventClass();
  
  [COLOR=green]// Register Cancel event[/color]
  ev.Cancel += [COLOR=blue]new[/color] EventHandler(YourMethod);
  [COLOR=green]...[/color]
   
  [COLOR=blue]public void[/color] YourMethod([COLOR=blue]object[/color] sender,
                         EventArgs e)
  {
    [COLOR=green]// Handle the event here[/color]
  }
}
You know, this is all very interesting, as it looks that you want to call the Cancel method of your class by clicking on a button on a form. If such form is not created from within your usercontrol class, I'm not sure how you would get a reference to it. Thus, instead of (or in addition to) using an event, I would use a public method (in the usercontrol class) that could be called explicitly from anywhere the usercontrol object is visible. Thus, in the click event of your button, you would write something like:
Code:
YourUserControl.Terminate();
In this Terminate method (not event), I would write the logic that you want for the Cancel event, and it would be more or less the same thing.

Hope this helps!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
Hi,

Cheers for your help.

Catching the event is no problem - as you stated I would just place the code

EventClass ev = new EventClass();

// Register Cancel event
ev.Cancel += new EventHandler(YourMethod);

in the usercontrol class that is catching the event.

However, I am still having trouble throwing the event. For example when the button is pressed in usercontrol 1 the click event is thrown, and want to create another event called Cancel, which can be caught by usercontrol 2!

For some reason the the Cancel object is not associated with the eventhandler I define! Any ideas?
 
You can throw the event as you did in your first post, but in order for this to work, usercontrol1 must be declared from within usercontrol2. In usercontrol2, you would have something like this:
Code:
UserControl1 uc1 = [COLOR=blue]new[/color] UserControl1;
[COLOR=green]...[/color]
uc1.Cancel += [COLOR=blue]new[/color] EventHandler(...);
And then, throw the event from UserControl1 as you did in your first post, and UserControl2 will catch it.

However, I'm not sure how feasible this is, as both your UserControl objects would probably be created at design time. That's why I suggested that you use a method as opposed to an event and call the method explicitly.

Hope this helps!

JC


Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top