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

G

Guy Noir

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
 
B

Bruce Wood

Guy said:
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.

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.
 
B

Bruce Wood

Guy said:
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.

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.
 
G

Guy Noir

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
 
B

Bruce Wood

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....
 

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