动态创建Combobox的SelectedIndexChanged事件

时间:2013-09-24 12:29:21

标签: c# combobox

我正在设计一个Windows窗体应用程序。在这个应用程序中,用户从组合框中选择一个数字,然后根据数字,将创建一些动态控件(标签和组合框)。

我的问题是,我需要在这些动态创建的组合框“selectedindexchanged”事件上编写一些代码。但我不知道如何为动态组合框创建一个事件。

这是我的功能:

FORM1.CS

public void getchildCntrl(Panel pnl,ComboBox cmbb)
        {
            for (int ix = pnl.Controls.Count - 1; ix >= 0; ix--)
                if (pnl.Controls[ix].Name.Substring(0, 5) == "Child") pnl.Controls[ix].Dispose();

            if (cmbb.SelectedIndex != 0)
            {
                Label[] childLabels = new Label[cmbb.SelectedIndex];
                ComboBox[] txtTeamNames = new ComboBox[cmbb.SelectedIndex];
                for (int i = 0; i < txtTeamNames.Length; i++)
                {
                    //label create
                    var lbl = new Label();
                    childLabels[i] = lbl;
                    lbl.Name = "ChildLb" + i.ToString();
                    lbl.Text = (i + 1).ToString() + ". Çocuk-Yaş :";
                    lbl.Width = 80;
                    lbl.Location = new Point(cmbb.Location.X - 85, cmbb.Location.Y + 7 + ((i + 1) * 28));
                    lbl.Visible = true;
                    pnl.Controls.Add(lbl);

                    //combobox create
                    var cmb = new ComboBox();
                    txtTeamNames[i] = cmb;
                    cmb.Name = "Child" + i.ToString();
                    cmb.Location = new Point(cmbb.Location.X, cmbb.Location.Y + 5 + ((i + 1) * 28));
                    cmb.Width = 40;
                    cmb.DropDownStyle = ComboBoxStyle.DropDownList;
                    cmb.DataSource = ages.ToArray();
                    cmb.Visible = true;
                    pnl.Controls.Add(cmb);
                }
            }
        }

3 个答案:

答案 0 :(得分:2)

您只需注册以下活动......

cmb.SelectedIndexChanged += new System.EventHandler((object o, EventArgs e) => 
{
    //Do something here
});

或者

cmb.SelectedIndexChanged += new System.EventHandler(cmb_SelectedValueChanged);

private void cmb_SelectedValueChanged(object sender, EventArgs e)
{
    //Do something here.
}

答案 1 :(得分:1)

以这种方式注册事件处理程序:

cmb.SelectedIndexChanged+=new EventHandler(cmb_SelectedIndexChanged);

以这种方式取消注册:

cmb.SelectedIndexChanged-=new EventHandler(cmb_SelectedIndexChanged);

private void cmb_SelectedIndexChanged(object sender, System.EventArgs e)
{
    //write your event code here
}

How to: Create Event Handlers at Run Time for Windows Forms

public void getchildCntrl(Panel pnl,ComboBox cmbb)
    {

//// your code.....
                //combobox create
                var cmb = new ComboBox();
                cmb.SelectedIndexChanged+=new EventHandler(cmb_SelectedIndexChanged);
// remaining code
                cmb.Visible = true;
                pnl.Controls.Add(cmb);
            }
        }
    }

要指定参数 - 请执行:

ComboBox.SelectedIndexChanged Event

答案 2 :(得分:1)

您可以将事件处理程序连接到ComboBox,如下所示:

cmd.SelectionChanged += new SelectionChangedEventHandler(GuiController_SelectionChanged);

void GuiController_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   throw new NotImplementedException();
}