Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向面板内的控件动态添加事件处理程序_C#_Events_Dynamic_Panel_Handler - Fatal编程技术网

C# 向面板内的控件动态添加事件处理程序

C# 向面板内的控件动态添加事件处理程序,c#,events,dynamic,panel,handler,C#,Events,Dynamic,Panel,Handler,简而言之,我有一门课: 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();

简而言之,我有一门课:

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.
在另一个类中,我有一个面板列表,所有这些面板都是使用上述方法创建的。 布局完成了,工作也很整洁,我可以在屏幕上添加任意数量的内容。但是现在我想给这些控件添加一些函数。例如,我希望禁用所有单选按钮,除非该复选框已启用。
那么,我如何才能向面板列表中的所有复选框中添加一个复选更改事件呢?

您似乎只想知道如何将
EventHandler
s动态添加到控件中

可以这样做:

cb_choice.CheckedChanged += cb_choice_CheckedChanged;
或者,如果你想更明确(两种方法都是一样的)

并定义一个处理程序方法:

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

在复选框的事件处理程序中(值已更改或其他内容),是否可以执行此伪代码
如果启用,请禁用单选按钮
?这就是你的意思吗?我需要在面板列表的每个成员中动态地执行此操作的代码。你的问题描述对我来说有点模糊,无法真正回答这个问题--我不太确定你希望每个复选框如何与单选按钮具体交互。。。但是连接到每个复选框的事件应该起作用。我不会只给你代码,你必须先尝试一些东西。不要动态地创建接口,而是在你的项目中添加一个UserControl,并在设计时将所有这些控件放在上面。然后,您可以向UserControl添加代码……为什么要用“艰难的方式”这样做?
private void cb_choice_CheckedChanged(object o, EventArgs args)
{
    //add code to enable/disable radiobuttons
}