【问题标题】:How can i add item in listbox each time that an button was clicked?每次单击按钮时,如何在列表框中添加项目?
【发布时间】:2013-12-28 17:21:03
【问题描述】:

我有一个解决方案,当存在许多按钮和 1 个列表框时。当按下指定按钮时,我想在列表框中添加文本按钮。

由于有很多按钮,我使用了一个“功能”,可以让我显示一些按钮并隐藏其他按钮,以组织表单和程序。

为了显示其他按钮并隐藏第一个按钮,我构建了一个这样的算法:

//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);
    }
}

为了显示“饮料”按钮,我使用了以下代码:

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();
}

当按下 1 个按钮(或另一个)时,我该如何做类似的事情来在 listbox1 中添加文本。我试过了,但我没有成功。

【问题讨论】:

  • 是Window应用还是Web??
  • 你试过这个one吗?

标签: c# events button methods listbox


【解决方案1】:
    **Constructor**
    public Form1() 
    {
        InitializeComponent();
        button2.Click += new EventHandler(button1_Click);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        listBox1.Items.Add(textBox1.Text);
    }

【讨论】:

    猜你喜欢
    • 2015-09-25
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多