catch event on parent control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • antani

    #1

    catch event on parent control

    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.

  • Morten Wennevik [C# MVP]

    #2
    Re: catch event on parent control

    On Sat, 01 Sep 2007 11:32:02 +0200, antani <antani8791@yah oo.itwrote:
    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( ControlEventArg s e)
    {
    base.OnControlA dded(e);

    if(e.Control is CheckBox)
    ((CheckBox)e.Co ntrol).CheckedC hanged += new EventHandler(My GroupBox_Checke dChanged);
    else if(e.Control is NumericUpDown)
    ((NumericUpDown )e.Control).Val ueChanged += new EventHandler(My GroupBox_ValueC hanged);
    }

    protected override void OnControlRemove d(ControlEventA rgs e)
    {
    base.OnControlR emoved(e);
    if (e.Control is CheckBox)
    ((CheckBox)e.Co ntrol).CheckedC hanged -= new EventHandler(My GroupBox_Checke dChanged);
    else if (e.Control is NumericUpDown)
    ((NumericUpDown )e.Control).Val ueChanged -= new EventHandler(My GroupBox_ValueC hanged);
    }

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

    void MyGroupBox_Chec kedChanged(obje ct sender, EventArgs e)
    {
    // Add CheckBox code here
    }
    }

    --
    Happy coding!
    Morten Wennevik [C# MVP]

    Comment

    Working...