Raising event

  • Thread starter Thread starter Tomaz Koritnik
  • Start date Start date
T

Tomaz Koritnik

I want to raise an event (not call event handler!!) of a control. I have a
control and I need to raise it's Enter event programmatically so that
control will then call all it's Enter event handlers. I searched but
couldn't find anything and I can't call an event directly. I have to do this
because I need it when user switches between forms. In such a case, Enter
event is raised only on form getting focus but not on child control that is
focused. In Win32 I could at least send a message but I don't wanna use
messages in C#. I want a more .NET approach :).

regards
Tomaz
 
Tomaz,

You can't do this (it is considered a breach of security if you could).
If the control does not expose a method that will allow you to fire this,
then you won't be able to do it.

The only workaround I can think of would be to use reflection to get the
delegate stored internally, or to send a windows message to the control to
make it think it has to fire the event.

Or, you could create a control that derives from the appropriate control
and expose a method which will fire the event and use that where you need
to.

Hope this helps.
 
Just a quick note
You could also use reflection to access the protected method OnEnter and
invoke it. It fires the event.


Nicholas Paldino said:
Tomaz,

You can't do this (it is considered a breach of security if you could).
If the control does not expose a method that will allow you to fire this,
then you won't be able to do it.

The only workaround I can think of would be to use reflection to get the
delegate stored internally, or to send a windows message to the control to
make it think it has to fire the event.

Or, you could create a control that derives from the appropriate control
and expose a method which will fire the event and use that where you need
to.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Tomaz Koritnik said:
I want to raise an event (not call event handler!!) of a control. I have a
control and I need to raise it's Enter event programmatically so that
control will then call all it's Enter event handlers. I searched but
couldn't find anything and I can't call an event directly. I have to do
this because I need it when user switches between forms. In such a case,
Enter event is raised only on form getting focus but not on child control
that is focused. In Win32 I could at least send a message but I don't wanna
use messages in C#. I want a more .NET approach :).

regards
Tomaz
 
As Nicholas pointed out, the .NET designers went to great lengths to
make this near-impossible to do. There is probably a very good reason
for this.

This is one of those points at which you should post more details about
the problem you're trying to solve, rather than the solution you've
come up with (get the control to fire its Enter event... for what
purpose?) so we can suggest design alternatives.
 
As I said, I have several MDI forms wth controls on them and when form gets
focus, the control that also receives focus will not raise Enter event. I
need this in order to greatly simplify my code. I solved the problem by
using reflections and I'm calling OnEnter of the control to make it work.
It's not a bad design but if I would have to do it other way the design
would be more difficult. Also, I can't make descendants as many different
types of controls are used (text box, combo, list, grid,...) and I would
have to do lots of work to use my new controls in designe-mode.

regards
Tomaz
 

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


Back
Top