我需要动态创建按钮。但每个按钮需要不同的代码

时间:2015-11-18 13:38:11

标签: c# winforms button reset

我需要关于添加房间的计划。就像这样,当我按下添加按钮时,必须显示标签,文本框,复选框和按钮(以重置其他按钮)。但是当我动态创建一个按钮并按下重置时,它也会重置其他文本框和复选框。

我必须在“新按钮事件”中输入什么内容?

你们可以帮助我吗?

//这是我的代码

private void btnAdd_Click(object sender, EventArgs e) {

    Label label = new Label();
    int count = tableLayoutPanel1.Controls.OfType<Label>().ToList().Count;
    label.Location = new Point(10, (25 * count) + 2);
    label.Size = new Size(40, 20);
    label.Name = "lbl" + (count + 1);
    label.Text = "Kamer " + (count + 1);
    label.Anchor = AnchorStyles.None;

    tableLayoutPanel1.Controls.Add(label);

    TextBox textbox = new TextBox();
    int count1 = tableLayoutPanel1.Controls.OfType<TextBox>().ToList().Count;
    textbox.Location = new System.Drawing.Point(60, 25 * count1);
    textbox.Size = new System.Drawing.Size(80, 20);
    textbox.Name = "textbox" + (count1 + 1);
    textbox.Anchor = AnchorStyles.None;
    tableLayoutPanel1.Controls.Add(textbox);

    CheckBox checkbox = new CheckBox();
    int count2 = tableLayoutPanel1.Controls.OfType<CheckBox>().ToList().Count;
    checkbox.Location = new System.Drawing.Point(60, 25 * count2);
    checkbox.Size = new System.Drawing.Size(80, 20);
    checkbox.Name = "checkbox" + (count2 + 1);
    checkbox.Anchor = AnchorStyles.None;
    tableLayoutPanel1.Controls.Add(checkbox);

    Button button = new Button();
    int count3 = tableLayoutPanel1.Controls.OfType<Button>().ToList().Count;
    button.Location = new Point(10, (25 * count3) + 2);
    button.Size = new Size(140, 25);
    button.Name = "button" + (count3 + 1);
    button.Text = "Reset";
    button.Anchor = AnchorStyles.None;
    button.Click += new System.EventHandler(this.Button_Click);
    tableLayoutPanel1.Controls.Add(button);
}

0 个答案:

没有答案