[英]How can i add item in listbox each time that an button was clicked?
I've an solution, when exists many buttons and 1 listbox. 我有一个解决方案,当存在许多按钮和1个列表框时。 Happens that i want add textbutton in listbox when an specified button is pressed.
发生我想要在按下指定按钮时在列表框中添加textbutton的情况。
Due to have many buttons, i've used an "function" that allow me show some buttons and hide others buttons, this to organize form and program. 由于有许多按钮,我使用了“功能”,可以显示一些按钮而隐藏其他按钮,这可以组织表单和程序。
To show other buttons and hide the first buttons i builted an algorithm like this: 为了显示其他按钮并隐藏第一个按钮,我构建了如下算法:
//Event
public event EventHandler ButtonClick;
public event EventHandler ButtonClick1;
void b1_Click(object sender, EventArgs e)
{
if (ButtonClick != null)
{
ButtonClick(this, EventArgs.Empty);
}
}
void b2_Click(object sender, EventArgs e)
{
if (ButtonClick1 != null)
{
ButtonClick1(this, EventArgs.Empty);
}
}
To show "drinks" buttons i used this code: 为了显示“饮料”按钮,我使用了以下代码:
public void show_drinks(Button b11, Button b12)
{
b11.Text = type[0].ToString();
b11.Click += new EventHandler(b2_Click);
b12.Click += new EventHandler(b3_Click);//beverages
b12.Text = type[1].ToString();
}
How can i do an similar thing to add text in listbox1 when 1 button (or another) is pressed. 当按下1个按钮(或另一个按钮)时,如何做类似的事情在listbox1中添加文本。 I've tried but i'm not succeeding.
我已经尝试过,但没有成功。
**Constructor**
public Form1()
{
InitializeComponent();
button2.Click += new EventHandler(button1_Click);
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.