Calling events in c#??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does anyone know of a way to call events directly in c#, in the way you can
use the RaiseEvent command in VB.net?

I have a form (a custom soft input panel), which contains a member variable
of type TextBox. When you enter a text box on any main application form, the
text box passes itself to the SIP form, and shows the SIP form.

The SIP form then uses an event handler from it's own buttons (the input
keys) to append or delete text from the textbox (ie. you click the '7' button
on the SIP form, and '7' is appended to the text box on the application
form). This works in the same way as the pocket pc/win ce SIP. However, i
also need to fire events of the text boxes passed in, so that the validation
can be carried out. For example, i place the validation in the Key Up event
handler of the text box on the main application form, and then within the SIP
form, i can fire the Key Up event of the text box.

However, you cannot do this in C#. You can call an event of a class
directly only from within the class itself.

Therefore, i then tried extending the TextBox class into TextBoxExt, and in
TextBoxExt added a method FireKeyUp. I can then call the FireKeyUp method of
my extended class which calls the KeyUp event of its base class.

This is OK in theory (and works perfectly in the full framework), but when I
change the definition in my main form's text box from
System.Windows.Forms.TextBox to TextBoxExt - it disappears from the designer.
I think this is because of the compact framework's lack of support for
designing user controls, etc.

So if i was in VB.net, i could use RaiseEvent, and if I was in the full
framework in c#, i could use a user control/inherited class.

Any suggestions welcome!

Thanks.
 
Simply call the event.

delegate void EventType();

class MyClass
{
public event EventType MyEvent
...
// raise the event
MyEvent();
}

-Chris
 
Thanks Chris.

That's calling an event from within the class which contains the event.
What I'm trying to do is call the text box's own event from my sipForm class
which contains the text box as a member:


class sipForm()
{
TextBox currentTextBox

private void ProcessKeyUp()
{
currentTextBox.KeyUp(); <<<
}
}

[ i have left out the keydata arguments ]


The currentTextBox member variable of the sipForm is a reference to a text
box passed in from another form. Therefore it can be any text box on any
form, with its own implementation of keyUp (it may even have more than one
event handler method assigned within it's own form).

What i had tried was to create an extended TextBox which contains a method
to call the KeyUp event of it's base class. This is allowed as the event can
be called from within it's own class:

public class TextBoxExt : TextBox
{
....

public void FireKeyUp()
{
this.KeyUp();
}
}


then the text box definition and ProcessKeyUp in the sipForm would be
changed to...

TextBoxExt currrentTextBox; <<<

private void ProcessKeyUp()
{
currentTextBox.FireKeyUp(); <<<
}

.... and this works in the full framework. But when i change the references
on the other forms from TextBox to TextBoxExt i lose them from the designer.

What i'm trying to do is avoid having to explicitly call methods by name, or
assign eventhandlers for every text box in the app to delegates and events in
the sipform. I have an interface which must be implemented by every form in
the app, which means every form in the app contains a method which assigns an
event handler to the 'GotFocus' event of every text box on the form. When
the user clicks into a textbox, the method called by this event handler
passes the text box to the sipForm then positions and displays the sipForm.

Therefore if the sipForm can simply tell the textbox to fire it's own KeyUp
event (which in turn will execute what ever implementation that text box
has), then no further coding is needed for existing and future text boxes in
the app to provide sipForm support.

I have done and tested this the full framework. Maybe i'm trying to make it
too abstract and it's not possible, but i would have thought if vb can do it
then c# can.

It has been suggested to me to use Reflection to determine the methods to
use, but from what i've looked at today, Reflection seems to be more
concerned with classes and types (ie. you can get all the methods/events of a
class), whereas i'm concerned with instances. I know you can instantiate the
class and fire a method, but then it wouldn't be the right instance, just a
new one. I also can't see how to get EventInfo, then get the methods
assigned to it. There is a GetRaiseMethod method in the EventInfo class, but
it looks like that's just for you to override in derived classes - doesn't
work for TextBox. Although i'm not really familiar with Reflection...


Sorry if that's a bit long-winded.

Ian.
 
There is probably an even easier way than this, but you can use a little
reflection e.g.:-
using System.Reflection;

MethodInfo method = typeof(Control).GetMethod("OnKeyUp",
BindingFlags.NonPublic | BindingFlags.Instance);

method.Invoke(textBox1, new object[]{new KeyEventArgs(Keys.Enter)});



Obviously replace textBox1 with your textbox control reference. This will
simulate an Enter key up event. You can apply the same technique to other
events from outside the control.

Peter


--
Peter Foot
Windows Embedded MVP
http://www.inthehand.com | http://blog.opennetcf.org/pfoot/

Ian said:
Thanks Chris.

That's calling an event from within the class which contains the event.
What I'm trying to do is call the text box's own event from my sipForm
class
which contains the text box as a member:


class sipForm()
{
TextBox currentTextBox

private void ProcessKeyUp()
{
currentTextBox.KeyUp(); <<<
}
}

[ i have left out the keydata arguments ]


The currentTextBox member variable of the sipForm is a reference to a text
box passed in from another form. Therefore it can be any text box on any
form, with its own implementation of keyUp (it may even have more than one
event handler method assigned within it's own form).

What i had tried was to create an extended TextBox which contains a method
to call the KeyUp event of it's base class. This is allowed as the event
can
be called from within it's own class:

public class TextBoxExt : TextBox
{
...

public void FireKeyUp()
{
this.KeyUp();
}
}


then the text box definition and ProcessKeyUp in the sipForm would be
changed to...

TextBoxExt currrentTextBox; <<<

private void ProcessKeyUp()
{
currentTextBox.FireKeyUp(); <<<
}

... and this works in the full framework. But when i change the
references
on the other forms from TextBox to TextBoxExt i lose them from the
designer.

What i'm trying to do is avoid having to explicitly call methods by name,
or
assign eventhandlers for every text box in the app to delegates and events
in
the sipform. I have an interface which must be implemented by every form
in
the app, which means every form in the app contains a method which assigns
an
event handler to the 'GotFocus' event of every text box on the form. When
the user clicks into a textbox, the method called by this event handler
passes the text box to the sipForm then positions and displays the
sipForm.

Therefore if the sipForm can simply tell the textbox to fire it's own
KeyUp
event (which in turn will execute what ever implementation that text box
has), then no further coding is needed for existing and future text boxes
in
the app to provide sipForm support.

I have done and tested this the full framework. Maybe i'm trying to make
it
too abstract and it's not possible, but i would have thought if vb can do
it
then c# can.

It has been suggested to me to use Reflection to determine the methods to
use, but from what i've looked at today, Reflection seems to be more
concerned with classes and types (ie. you can get all the methods/events
of a
class), whereas i'm concerned with instances. I know you can instantiate
the
class and fire a method, but then it wouldn't be the right instance, just
a
new one. I also can't see how to get EventInfo, then get the methods
assigned to it. There is a GetRaiseMethod method in the EventInfo class,
but
it looks like that's just for you to override in derived classes - doesn't
work for TextBox. Although i'm not really familiar with Reflection...


Sorry if that's a bit long-winded.

Ian.



Simply call the event.

delegate void EventType();

class MyClass
{
public event EventType MyEvent
...
// raise the event
MyEvent();
}

-Chris
 
Thanks Peter - that works perfectly.

Also, looking at vb again, RaiseEvent myEvent() doesn't actually allow you
to call events from outside a class - it's just the same as calling myEvent()
in c#. Don't know what i was looking at before.

Ian.


Peter Foot said:
There is probably an even easier way than this, but you can use a little
reflection e.g.:-
using System.Reflection;

MethodInfo method = typeof(Control).GetMethod("OnKeyUp",
BindingFlags.NonPublic | BindingFlags.Instance);

method.Invoke(textBox1, new object[]{new KeyEventArgs(Keys.Enter)});



Obviously replace textBox1 with your textbox control reference. This will
simulate an Enter key up event. You can apply the same technique to other
events from outside the control.

Peter


--
Peter Foot
Windows Embedded MVP
http://www.inthehand.com | http://blog.opennetcf.org/pfoot/

Ian said:
Thanks Chris.

That's calling an event from within the class which contains the event.
What I'm trying to do is call the text box's own event from my sipForm
class
which contains the text box as a member:


class sipForm()
{
TextBox currentTextBox

private void ProcessKeyUp()
{
currentTextBox.KeyUp(); <<<
}
}

[ i have left out the keydata arguments ]


The currentTextBox member variable of the sipForm is a reference to a text
box passed in from another form. Therefore it can be any text box on any
form, with its own implementation of keyUp (it may even have more than one
event handler method assigned within it's own form).

What i had tried was to create an extended TextBox which contains a method
to call the KeyUp event of it's base class. This is allowed as the event
can
be called from within it's own class:

public class TextBoxExt : TextBox
{
...

public void FireKeyUp()
{
this.KeyUp();
}
}


then the text box definition and ProcessKeyUp in the sipForm would be
changed to...

TextBoxExt currrentTextBox; <<<

private void ProcessKeyUp()
{
currentTextBox.FireKeyUp(); <<<
}

... and this works in the full framework. But when i change the
references
on the other forms from TextBox to TextBoxExt i lose them from the
designer.

What i'm trying to do is avoid having to explicitly call methods by name,
or
assign eventhandlers for every text box in the app to delegates and events
in
the sipform. I have an interface which must be implemented by every form
in
the app, which means every form in the app contains a method which assigns
an
event handler to the 'GotFocus' event of every text box on the form. When
the user clicks into a textbox, the method called by this event handler
passes the text box to the sipForm then positions and displays the
sipForm.

Therefore if the sipForm can simply tell the textbox to fire it's own
KeyUp
event (which in turn will execute what ever implementation that text box
has), then no further coding is needed for existing and future text boxes
in
the app to provide sipForm support.

I have done and tested this the full framework. Maybe i'm trying to make
it
too abstract and it's not possible, but i would have thought if vb can do
it
then c# can.

It has been suggested to me to use Reflection to determine the methods to
use, but from what i've looked at today, Reflection seems to be more
concerned with classes and types (ie. you can get all the methods/events
of a
class), whereas i'm concerned with instances. I know you can instantiate
the
class and fire a method, but then it wouldn't be the right instance, just
a
new one. I also can't see how to get EventInfo, then get the methods
assigned to it. There is a GetRaiseMethod method in the EventInfo class,
but
it looks like that's just for you to override in derived classes - doesn't
work for TextBox. Although i'm not really familiar with Reflection...


Sorry if that's a bit long-winded.

Ian.



Simply call the event.

delegate void EventType();

class MyClass
{
public event EventType MyEvent
...
// raise the event
MyEvent();
}

-Chris


Does anyone know of a way to call events directly in c#, in the way you
can
use the RaiseEvent command in VB.net?

I have a form (a custom soft input panel), which contains a member
variable
of type TextBox. When you enter a text box on any main application
form,
the
text box passes itself to the SIP form, and shows the SIP form.

The SIP form then uses an event handler from it's own buttons (the
input
keys) to append or delete text from the textbox (ie. you click the '7'
button
on the SIP form, and '7' is appended to the text box on the application
form). This works in the same way as the pocket pc/win ce SIP.
However,
i
also need to fire events of the text boxes passed in, so that the
validation
can be carried out. For example, i place the validation in the Key Up
event
handler of the text box on the main application form, and then within
the
SIP
form, i can fire the Key Up event of the text box.

However, you cannot do this in C#. You can call an event of a class
directly only from within the class itself.

Therefore, i then tried extending the TextBox class into TextBoxExt,
and
in
TextBoxExt added a method FireKeyUp. I can then call the FireKeyUp
method
of
my extended class which calls the KeyUp event of its base class.

This is OK in theory (and works perfectly in the full framework), but
when
I
change the definition in my main form's text box from
System.Windows.Forms.TextBox to TextBoxExt - it disappears from the
designer.
I think this is because of the compact framework's lack of support for
designing user controls, etc.

So if i was in VB.net, i could use RaiseEvent, and if I was in the full
framework in c#, i could use a user control/inherited class.

Any suggestions welcome!

Thanks.
 
Back
Top