简体   繁体   English

如何创建专属面板

[英]How to create exclusive panel

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. 顺便说一句,我正在使用.NETCF。

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 以编程方式添加的内容将由您的AddControl处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM