2013-05-02 92 views
0

我一类,这是一言以蔽之:添加事件处理动态地控制面板内

class MyPanel 
{ 
    Panel p_panel;      //declaring all the elements I need 
    CheckBox cb_choice; 
    RadioButton rb_nagy, rb_kicsi; 
    TextBox tb_db; 

    public Panel getPanel() {   
     create();      //creating all the elements I need, then putting them all in the created Panel. 
     customize();     //setting the panel's size, and the other control's locations within the panel. 
     return p_panel;    //so if I call this method from an other class, this method will return a panel with all the controls inside. 

在其他类中,我有所有这一切已经与创建小组名单以上方法。 布局完成,它工作整齐,我可以添加尽可能多的我想要的屏幕。但现在我想为这些控件添加一些功能。例如,除非启用复选框,否则我希望所有单选按钮都被禁用。 那么如何将检查更改的事件添加到面板列表中的所有复选框?

+0

在该复选框事件处理程序(值改变,或者不管它是什么),你可以这样做伪'如果启用,禁用无线电buttons'?你是这个意思吗? – tnw 2013-05-02 17:48:30

+0

我需要能够动态地在面板列表的每个单独成员中执行此操作的代码。 – 2013-05-02 17:59:10

+0

您的问题描述对我来说确实有点含糊 - 我不确定您希望每个复选框如何与单选按钮进行专门交互......但是连接到每个复选框的事件都应该做到这一点。我不会只给你代码,你必须先尝试一下。 – tnw 2013-05-02 18:00:50

回答

0

看起来你只是想知道如何动态地将EventHandler s添加到你的控件!?

这是可以做到这样的:

cb_choice.CheckedChanged += cb_choice_CheckedChanged; 

,或者如果你想更明确的(两者的工作方式相同)。

cb_choice.CheckedChanged += new EventHandler(cb_choice_CheckedChanged); 

,并定义一个处理方法:

private void cb_choice_CheckedChanged(object o, EventArgs args) 
{ 
    //add code to enable/disable radiobuttons 
}