catch event on parent control

A

antani

I have a groupbox in my application that contains many control as
NumericUpDown and CheckBox. I would like capture event on groupbox
control as changing NumericUpDown value and checkbox change .
How can do it?

Thanks.
 
M

Morten Wennevik [C# MVP]

I have a groupbox in my application that contains many control as
NumericUpDown and CheckBox. I would like capture event on groupbox
control as changing NumericUpDown value and checkbox change .
How can do it?

Thanks.

Hi Antani,

Not sure where you are going with this, but you can create your own GroupBox and add appropriate event handlers whenever you add a control to it.. The sample GroupBox below attaches an event handler for every CheckBox and NumericUpDown control added, in the designer or otherwise. If youneed to know which specific control fired an event, use the sender property.

public class MyGroupBox : GroupBox
{
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);

if(e.Control is CheckBox)
((CheckBox)e.Control).CheckedChanged += new EventHandler(MyGroupBox_CheckedChanged);
else if(e.Control is NumericUpDown)
((NumericUpDown)e.Control).ValueChanged += new EventHandler(MyGroupBox_ValueChanged);
}

protected override void OnControlRemoved(ControlEventArgs e)
{
base.OnControlRemoved(e);
if (e.Control is CheckBox)
((CheckBox)e.Control).CheckedChanged -= new EventHandler(MyGroupBox_CheckedChanged);
else if (e.Control is NumericUpDown)
((NumericUpDown)e.Control).ValueChanged -= new EventHandler(MyGroupBox_ValueChanged);
}

void MyGroupBox_ValueChanged(object sender, EventArgs e)
{
// Add NumericUpDown code here
}

void MyGroupBox_CheckedChanged(object sender, EventArgs e)
{
// Add CheckBox code here
}
}
 

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