overriding events in a UserControl

D

Dom

When I create a UserControl, I inherit certain events, in particular,
the TextChanged and KeyDown events. Yet I can't seem to capture them,
and I think I know why. If my UserControl contains a TextBox and a
ListBox, which one fires the KeyDown event? or the TextChanged event?

Are these events just there for me to override, if I need to? If so,
is this the right way:

public delegate void KeyDownDelegate(object sender,
KeyEventArgs e);
public new event KeyDownDelegate KeyDown;
protected override void OnKeyDown(KeyEventArgs e)
{
if (KeyDown != null) KeyDown(this, e);
}

Notice the "new" in the second line, and the "override" in the third
line. I did this by hit or miss. Another possibility is to NOT
declare anything at all, but just call OnTextChanged and OnKeyDown as
I need them.

Which is the right way?
 
D

Dom

Neither.  The UserControl is a Control instance on its own, and behaves
like any other Control, including having its own KeyDown, TextChanged,
etc. events.  They are raised by the UserControl's own window procedure,
just as any other Control would do.

When dealing with a parent-child relationship, as UserControl has with
Control instances contained within, there's a hierarchy of message
handling, just as there is for Control instances within a Form (very
common), or Control instances within any other Control (only common when
dealing with container Control classes, but can actually happen for any
Control type).









It depends on what you're trying to do.  What are you trying to do?  What
effect do you really want to have?

Hiding the base class event (by declaring a "new event" of the same name)
is almost certainly not the correct approach, but beyond that it's hard to
say what you should actually do without knowing what the ultimate goal
here is.

Pete- Hide quoted text -

- Show quoted text -

My usercontrol has a text box and a listbox. It is a kind of ComboBox
but with certain features that I can't get just by extending the usual
ComboBox. For example, the forecolor of the textbox will change if
the user types something in the textbox that is not found in the list
box. And so on. I want to capture the Keydown event and the
TextChanged event of the textbox, and pass them on to the parent, so
the parent can take the appropriate action when the user hits a CR.

So far, I am just calling OnKeyDown which comes with the User
Control. For some reason, I can't do that with OnTextChanged.


Dom
 
D

Dom

[...] I want to capture the Keydown event and the
TextChanged event of the textbox, and pass them on to the parent, so
the parent can take the appropriate action when the user hits a CR.
So far, I am just calling OnKeyDown which comes with the User
Control.  For some reason, I can't do that with OnTextChanged.

I don't understand why you aren't just subscribing to the events of the  
TextBox control from the UserControl.  Why are you messing with the On....  
methods at all?

Pete

Either I'm confused (very likely) or I didn't explain myself correctly
(also likely).

One more shot ...

I can of course, subscribe to the TextBox events WITHIN the
UserControl. But the form that uses the UserControl can not. Or that
is my understanding.

For example, in MyUserControl.cs, I can capture MyTextBox_KeyDown.
But now in MyForm, in which I have placed MyUserControl, I want to get
MyUserControl_KeyDown. This is done because in one application, I
need to fire off a routine when the user hits CR, but in another
application, I wait for the user to hit Cntrl-CR. And so on.

As far as I know, I need to capture (within MyUserControl) the
MyTextBox_KeyDown event, and use this to fire another event that is
captured by MyForm.

I hope that's clear.

Dom
 

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