动态创建一个按钮

时间:2014-09-14 14:45:26

标签: c# winforms button dynamic

在Visual Studio C#窗口中,如何在每次将项目插入列表框时动态创建按钮?

单击创建的按钮时,必须从列表框中删除插入的项目。

我想添加按钮:

 if (comboBox4.Text != "" && listBox1.Text != "" && comboBox3.Text != "")
  {
     string ha = listBox1.SelectedItem.ToString();
     Clipboard.SetText(comboBox4.Text + "stk " + ha + " i farve " + comboBox3.Text);
     listBox2.Items.Add(comboBox4.Text + "stk " + listBox1.SelectedItem.ToString() +
                        " " + comboBox3.Text);
  }

1 个答案:

答案 0 :(得分:0)

创建一个按钮并添加到表单。

伪代码(不是完整的解决方案,只有它背后的主要登录名):

public class MyEventArgs: EventArgs
{
    public ListBoxValue lbv;
}

...
listBox2.Items.Add(comboBox4.Text + "stk " + listBox1.SelectedItem.ToString() + " " + comboBox3.Text);

Button x = new Button();
x.Text = "whatever";
x.Top = some coordinate;
x.Left = some coordinate;

MyEventArgs me = new MyEventArgs();
me.lbv = inserted lisbox item reference;

x.Click += new ClickHandler(this, me);
myForm.Controls.Add(x);

...

private void ClickHandler(object sender, MyEventArgs e)
{
    listbox.Items.Remove(e.lbv);
}