Raising an event outside of the class

G

Guest

Hi,

I have searched on the net quite a bit, but the more I read, the more
confused I get.

I want to see if I can raise an event out side of the class. I believe this
can be done in VB (at least VB 6.0) by calling raise event ....

For example,

Say if I have a button control, and wrote a handler to handle the "Click"
event, say "private void MyButton_Click(...)" with in a form, say "MyForm".

In another method "another_method()"with in class "MyFrom",

private void another_method()
{
//Raise MyButton Click event
}

I can either
1. Call MyButton_Click(). This may not be a good idea since I if the Click
event is assigned more than 1 method, I am not "correctly" simulating the
Click event, unless I manually call the rest.

2. Somehow, I simulate raising a button click event as an actual mouse click
will do.


How can I achieve (2)?

Thanks,
 
D

DBC User

Geroge,

I know you are looking for option (2) but in my code, I call the click
event directly and inside I check and see if it is initiated through
the code or by the actual event.

Thanks.
 
J

Jeffrey Tan[MSFT]

Hi George,

Thanks for your post!

Yes, event can only be directly fired in the class that defines that event,
C# compiler does not allow us to fire it outside of the class, below is the
sample code snippet to demonstrate this:

You can fire a self defined event like this:
public event EventHandler MyEvent;
private void button1_Click(object sender, System.EventArgs e)
{
this.MyEvent+=new EventHandler(Form1_MyEvent);
this.MyEvent(this, null);
}

private void Form1_MyEvent(object sender, EventArgs e)
{
MessageBox.Show("abc");
}

If you place this event in a class and try to fire it outside this class,
the compiler will generate an error:
private void button2_Click(object sender, System.EventArgs e)
{
MyEventClass obj=new MyEventClass();
obj.MyEvent+=new EventHandler(obj_MyEvent);
obj.MyEvent(this, null);
//this line will generate compiler error:
//The event 'AsynchronousTest.Form1.MyEventClass.MyEvent' can only appear
on
//the left hand side of += or -=
//(except when used from within the type
'AsynchronousTest.Form1.MyEventClass')
}

public class MyEventClass
{
public event EventHandler MyEvent;
}

private void obj_MyEvent(object sender, EventArgs e)
{
MessageBox.Show("abc");
}

To access an event outside of a class, the class designer must expose some
public methods for us to trigger. In .Net winform, the GUI event will
follow with a On[event] method, which fires the corresponding event.
However, this method is always protected, which can only be accessed
outside(except child class certainly). Fortunately, Button.PerformClick()
public method internally calls OnClick method to trigger Click event, so
you can invoke this method to achieve what you want. For other GUI events,
you might have to simulate the corresponding GUI messages to trigger the
event

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi George,

Have you got my reply? Does it make sense to you? Please feel free to tell
me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Similar Threads


Top