0

I would like to create a panel where only one control can be enabled at a time. It should work just the way radio buttons do.

The idea is something like that:

class XClusivePanel : Panel
{
    // Init code

    // Use this in order to add Exclusive Controls
    void AddControl( Control c )
    {
        if(! Controls.Contains(c) )
        {
            Controls.Add(c);

            c.Enabled = false;

            c.EnabledChanged += new System.EventHandler( this.control_EnabledChanged );
        }
    }

    // Avoid more than one control enabled at a time.
    private void control_EnabledChanged( object sender, EventArgs e )
    {
        Control s = (Control)sender;

        if(s.Enabled == true)
        {
            foreach( Control c in Controls )
            {
                if( s != c )
                {
                    s.Enabled = false;  
                }
            }
        }
    }
}

The problem with this code is that it works only if you create your form by code; if you add components using the designer, it does not work.

Any idea? By the way, I am working with the .NET CF.

Cristiano
  • 856
  • 10
  • 24
  • How does a user switch controls if everything else is disabled? – LarsTech Mar 16 '12 at 19:55
  • @LarsTech: the flow of the program will enable a control when needed. But, when I will do a someControl.Enabled = true somewhere in the code, it should automatically disable the previously active control. – Cristiano Mar 16 '12 at 19:58

1 Answers1

0

Why you don't use the form load event(or similar) to disable all the controls on the form (including the one added by the designer). The ones added programmatically would be then taken care by your AddControl

Kharaone
  • 597
  • 4
  • 16
  • Yeah, that would be a solution, but if it's possibile I would like to have this work performed by the panel itself, without having the form worry about that. – Cristiano Mar 17 '12 at 09:18
  • Just use ControlAdded event then with an handler that does your magic :) – Kharaone Mar 17 '12 at 14:28
  • Unfortunately, the ControlAdded event does not exist in the Compact Framework... :( – Cristiano Mar 19 '12 at 08:04