2013-05-02 90 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 
}