0

我正在设计一个 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);
                }
            }
        }
4

3 回答 3

3

您只需像下面这样注册活动...

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.
}
于 2013-09-24T12:35:07.590 回答
1

您可以像这样将事件处理程序连接到 ComboBox:

cmd.SelectionChanged += new SelectionChangedEventHandler(GuiController_SelectionChanged);

void GuiController_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   throw new NotImplementedException();
}
于 2013-09-24T12:33:17.830 回答
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
}

如何:在运行时为 Windows 窗体创建事件处理程序

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 事件

于 2013-09-24T12:31:28.800 回答