Raise an event?

  • Thread starter John Francisco Williams
  • Start date
J

John Francisco Williams

Hi All. How do I cause an event to be "manually" fired. I have a user
control that contains a button. What I need to do is this: When the user
clicks that button, the event "Click" for the user control that contains
that button, needs to be fired. I tried "RaiseMouseEvent", but there is
almost no documentation on that method. I'm using .Net 2.0 and, it looks
like "RaiseMouseEvent" is now "available for everyone" but, I don't know how
to use it (if I should use it at all). Thanks in advance for your help,

Frank.
 
K

Keith Elder

John said:
Hi All. How do I cause an event to be "manually" fired. I have a user
control that contains a button. What I need to do is this: When the user
clicks that button, the event "Click" for the user control that contains
that button, needs to be fired. I tried "RaiseMouseEvent", but there is
almost no documentation on that method. I'm using .Net 2.0 and, it looks
like "RaiseMouseEvent" is now "available for everyone" but, I don't know how
to use it (if I should use it at all). Thanks in advance for your help,

Frank.

Are you sure you want to create a Click event for the user control and
not the button? The button is after all what they are clicking right?

The simplest way is to just double click the button in the designer. If
there isn't a designer then do:

button1.Click += new EventHandler(button1_Click);

void button1_Click(object sender, EventArgs e)
{
// add code here
}

BTW, if you just start typing:

yourButtonObjectName.Click +=

then press tab, it will create the method for you.

Hope that helps.

-Keith
 
J

John Francisco Williams

Thank you. I need a click event on the user control. What I need is to
signal the user control consumer, that the user control has been clicked. I
could make public the button contained in the user control, and then go
UserControl.Button1.Click += ... , but I was looking for a cleaner approach.


John said:
Hi All. How do I cause an event to be "manually" fired. I have a user
control that contains a button. What I need to do is this: When the user
clicks that button, the event "Click" for the user control that contains
that button, needs to be fired. I tried "RaiseMouseEvent", but there is
almost no documentation on that method. I'm using .Net 2.0 and, it looks
like "RaiseMouseEvent" is now "available for everyone" but, I don't know
how
to use it (if I should use it at all). Thanks in advance for your help,

Frank.

Are you sure you want to create a Click event for the user control and
not the button? The button is after all what they are clicking right?

The simplest way is to just double click the button in the designer. If
there isn't a designer then do:

button1.Click += new EventHandler(button1_Click);

void button1_Click(object sender, EventArgs e)
{
// add code here
}

BTW, if you just start typing:

yourButtonObjectName.Click +=

then press tab, it will create the method for you.

Hope that helps.

-Keith
 
M

Marc Gravell

OK; First - to do what you want to:


Assuming that you are inheriting from UserControl, then your bespoke control
can just call the OnClick (protected) method, which will fire the base
(UserControl) Click event. It is a fairly standard pattern - the base has a
public event MyEvent, and a protected method OnMyEvent() which subclasses
can call to simulate this behaviour.

*however*. I personally don't think you should do this. The base
implementation of UserControl will, itself, fire the Click event whenever a
user clicks on the background area. This means that your callers could
receive misleading Click events. You could go to a lot of pain to prevent
this firing except when you want to, but I would be more inclined to publish
your *own* event (using the above public/protected pattern), and have
callers subscribe to that.

If you aren't inheriting from UserControl you'll be in a lot more of a
pickle ;-p You could probably do it through reflection (calling the
non-public OnClick method), but I wouldn't recommend it.

Marc
 
J

John Francisco Williams

Thank you. OnClick worked perfect. It's just what I needed. My control
Doesn't have exposed background. Everything is being used up by the controls
inside (it will be used in a grid-like fashion).

Thanks again,

Frank.

OK; First - to do what you want to:


Assuming that you are inheriting from UserControl, then your bespoke control
can just call the OnClick (protected) method, which will fire the base
(UserControl) Click event. It is a fairly standard pattern - the base has a
public event MyEvent, and a protected method OnMyEvent() which subclasses
can call to simulate this behaviour.

*however*. I personally don't think you should do this. The base
implementation of UserControl will, itself, fire the Click event whenever a
user clicks on the background area. This means that your callers could
receive misleading Click events. You could go to a lot of pain to prevent
this firing except when you want to, but I would be more inclined to publish
your *own* event (using the above public/protected pattern), and have
callers subscribe to that.

If you aren't inheriting from UserControl you'll be in a lot more of a
pickle ;-p You could probably do it through reflection (calling the
non-public OnClick method), but I wouldn't recommend it.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top