c#链接两个动态添加的控件。 (Windows表单应用程序)

时间:2014-09-28 05:56:42

标签: c# dynamic combobox

这是一个非常基本的问题,但我无法找到解决方案。

我有这个代码,当用户按下按钮时动态创建组合框和标签。我的qustion现在,如何链接组合框和标签,以便标签显示组合框中选择的内容?

// Tilføjer combobox
ComboBox cboRun = new ComboBox();
cboRun.Name = "cboDynamic1" + c++;
cboRun.Location = new System.Drawing.Point(10, 10 + (20 * c));
cboRun.Size = new System.Drawing.Size(200, 25);
cboRun.BringToFront();
cboRun.Enter += CBox_Enter;
grb_MealOne.Controls.Add(cboRun);

// Tilføjer label
Label labRun = new Label();
labRun.Name = "labDynamic1" + c;
labRun.Location = new System.Drawing.Point(270, 10 + (20 * c));
labRun.Size = new System.Drawing.Size(25, 25);
labRun.BringToFront();
labRun.Text = "Neee";
grb_MealOne.Controls.Add(labRun);

我真的不知道如何做这个部分。尝试了很多不同的东西!

1 个答案:

答案 0 :(得分:0)

Label L;
public void YourMethod()
{
    //Create the `ComboBox1` and set the event `comboBox1_SelectedIndexChanged` to it
    Label Y = new Label();
    // ...
    L = Y;
}
public void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    L.Text = "Something else";
}

此字段(L)是您的Label,您可以在任何地方使用它。

相关问题