Passing Events from a user control to a form using the user control

Passing Events from a user control to a form using the user control

Post by Guy Noi » Wed, 04 Oct 2006 02:25:22


I have a quick question on custom controls.

I have a control that I created with a class that extends the
NumericUpDown control.

My problem is that when I use this usercontrol on my form, the click
event gets handled by the user class. I want the event to be passed to
the Windows form (not the user control).

So I am guessing that I need to raise an event from the user control
click event

User Control Click -> Gets handled by the user control method:
private void NumericUpDownControl_Click(object sender, EventArgs e)
{
// Raise an event that I can process on the calling form??
// In other words, I need the click event on the user
control to trigger a method on
// the Windows Form that is using the control.
}

Does this make sense?
Am I on the right track here? Please bear with me as this is my first
attempt at a custom control.

Thanks!
-Guy
 
 
 

Passing Events from a user control to a form using the user control

Post by Bruce Woo » Wed, 04 Oct 2006 04:57:36


Yes, you are on exactly the right track. The only thing that you need
to clarify is what message you're attempting to transmit to the
enclosing form. In other words, what does this use click mean in
higher-level terms? Why does the enclosing form want to know? If you
know that then you'll know exactly when to raise the event and what to
name it.

 
 
 

Passing Events from a user control to a form using the user control

Post by Bruce Woo » Wed, 04 Oct 2006 05:34:29


Yes, you are on exactly the right track. The only thing that you need
to clarify is what message you're attempting to transmit to the
enclosing form. In other words, what does this use click mean in
higher-level terms? Why does the enclosing form want to know? If you
know that then you'll know exactly when to raise the event and what to
name it.
 
 
 

Passing Events from a user control to a form using the user control

Post by Guy Noi » Wed, 04 Oct 2006 06:31:21

Thanks for the reply Bruce.

What I want to know exactly is when the control is clicked. When this
control is clicked, I want to update a text field in the form with the
value of the user control.

Thanks!
-Guy
 
 
 

Passing Events from a user control to a form using the user control

Post by Bruce Woo » Wed, 04 Oct 2006 07:23:39

It sounds to me as though you want to know when the control's value has
changed, not whenever it is clicked. That's why I asked: I wasn't
convinced that it wasn't a Click that you were after, but something
else.

So, maybe you want something like:

public event System.EventHandler ValueChanged;

private void NumericUpDownControl_ValueChanged(object sender,
System.EventArgs e)
{
if (this.ValueChanged != null)
{
this.ValueChanged(this, System.EventArgs.Empty);
}
}

which assumes, of course, that your user control exposes a
corresponding Value property....
 
 
 

Passing Events from a user control to a form using the user control

Post by Guy Noi » Thu, 05 Oct 2006 07:45:22

Bruce, worked like a charm!

Thanks so much for your valuable input!!

-Guy