How to manualy initiate the event?

H

Haim

Hi,
I would like to know if there in C# an equivalent to the
VB.NET RaiseEvent statement.

I would like to raise an event of a control in my code as
if the user's action provoked it.

Thanks,

Haim.
 
P

Peter Jausovec

Sure. E.g. :

private void button_Click(object sender, System.EventArgs e)
{
// do something here
}

private void RaiseTheEvent (...)
{
button_Click (this, System.EventArgs)
}
 
S

Sijin Joseph

Let's suppose you have an event called DirtyChanged in your class, here
is how you would raise the event using C#

public class EventTester
{
public event EventHandler DirtyChanged;

public void RaiseDirtyChangedEvent()
{
//The check is required because if no event handlers are
//attached to the event then the next line will throw a
//NullReferenceExcpetion

if(DirtyChanged != null)
DirtyChanged(this,new EventArgs());
}
}

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
 
M

Morten Wennevik

Hi haim,

If you have a MouseDown event like this

private void MyControl_MouseDown(object sender, MouseEventArgs e)
{
}

then you can call this directly with

MyControl_MouseDown(MyControl, new MouseEventArgs(MouseButtons.Right, 1, x, y, 0));

To indicate that the user clicked once on MyControl in location (x, y);
 
H

Haim

My question was - if you have a control with a given set
of events, like a notifyIcon that displayes the text only
when on the mouse hover event, its not me who wrote this
event function (it is a part of the given control and I
have no access to it) so I can't call it.
I would like to trigger the mouse hover event of a
notifyIcon (for example) at my will and not only when the
user activate it.

Is there a way?
 
G

Guest

Please see my above reply to Peter, your solution does
not help me...
-----Original Message-----
Hi haim,

If you have a MouseDown event like this

private void MyControl_MouseDown(object sender, MouseEventArgs e)
{
}

then you can call this directly with

MyControl_MouseDown(MyControl, new MouseEventArgs
(MouseButtons.Right, 1, x, y, 0));
 
G

Guest

your solution does not help me. Please see my reply to
peter with subject: "OK, I knew that already".

Thanks,

Haim.
 
P

Peter Jausovec

If I understand you're trying to show the tooltip for e.g. notifyIcon
programmatically ?
 
S

Sijin Joseph

Hi Haim,

There is no clean way to do this other than deriving from the control.
But there is a hack that you could use, using reflection. To fire the
event you need to call the control OnXXX() methods for the corresponding
event. For e.g to fire the Hover event of the button control you will
need to call the OnMouseHover protected method of the button class.

Here is a sample code i wrote to invoke the click event of a button

MethodInfo mi =
this.button1.GetType().GetMethod("OnClick",BindingFlags.Instance|BindingFlags.NonPublic);
if(mi != null)
mi.Invoke(button1,new object[]{new EventArgs()});

Hope that helps.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
 
H

Haim

But this is only a small example for a need of this kind.
In VB.NET the RaiseEvent statement solve this problem, I
thought there must be an equivalent in C#...

Thanks again,

Haim.
 
H

Haim

Reflection is a great idea! don't know how I didn't think
about it myself :)
Thanks a lot!!! this is very much appreciated!!!

Haim.
-----Original Message-----
Hi Haim,

There is no clean way to do this other than deriving from the control.
But there is a hack that you could use, using reflection. To fire the
event you need to call the control OnXXX() methods for the corresponding
event. For e.g to fire the Hover event of the button control you will
need to call the OnMouseHover protected method of the button class.

Here is a sample code i wrote to invoke the click event of a button

MethodInfo mi =
this.button1.GetType().GetMethod ("OnClick",BindingFlags.Instance|BindingFlags.NonPublic);
if(mi != null)
mi.Invoke(button1,new object[]{new EventArgs()});

Hope that helps.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


your solution does not help me. Please see my reply to
peter with subject: "OK, I knew that already".

Thanks,

Haim.



your class, here


event handlers are


line will throw a
code

as
.
 
G

Guest

BTW, RaiseEvents can not be used to raise events declared in other modules.
As the docs state: "You cannot use RaiseEvent to raise events that are not
explicitly declared in the module". Reflection will work or you can inherit
from the specific class and call the protected OnXxx method (if present) of
the event.

Regards, Jakob.


Haim said:
Reflection is a great idea! don't know how I didn't think
about it myself :)
Thanks a lot!!! this is very much appreciated!!!

Haim.
-----Original Message-----
Hi Haim,

There is no clean way to do this other than deriving from the control.
But there is a hack that you could use, using reflection. To fire the
event you need to call the control OnXXX() methods for the corresponding
event. For e.g to fire the Hover event of the button control you will
need to call the OnMouseHover protected method of the button class.

Here is a sample code i wrote to invoke the click event of a button

MethodInfo mi =
this.button1.GetType().GetMethod ("OnClick",BindingFlags.Instance|BindingFlags.NonPublic);
if(mi != null)
mi.Invoke(button1,new object[]{new EventArgs()});

Hope that helps.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


your solution does not help me. Please see my reply to
peter with subject: "OK, I knew that already".

Thanks,

Haim.


-----Original Message-----
Let's suppose you have an event called DirtyChanged in

your class, here

is how you would raise the event using C#

public class EventTester
{
public event EventHandler DirtyChanged;

public void RaiseDirtyChangedEvent()
{
//The check is required because if no

event handlers are

//attached to the event then the next

line will throw a

//NullReferenceExcpetion

if(DirtyChanged != null)
DirtyChanged(this,new EventArgs

());

}
}

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


Haim wrote:

Hi,
I would like to know if there in C# an equivalent to

the

VB.NET RaiseEvent statement.

I would like to raise an event of a control in my code

as

if the user's action provoked it.

Thanks,

Haim.


.
.
 

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