简体   繁体   English

每次单击按钮时,如何在列表框中添加项目?

[英]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.

相关问题 如何为listBox中的每个项目添加menuStrip菜单? - How can i add a menuStrip menus for each item in a listBox? 如何为列表框的每个项目添加“删除”按钮并实现其功能? - How to add a “Remove” button to each item of a ListBox and implement its functionality? 每次单击按钮时如何添加新字段 - How to add a new field each time a button is clicked 每次单击“注册”按钮时如何用表格信息填充列表框(无BD) - How to fill a listBox with the information of a form each time the 'register' button is clicked (Without BD) 如何为列表框中的每个项目设置不同的工具提示文本? - How can I set different Tooltip text for each item in a listbox? 我如何向该程序中添加鼠标单击,每次在C#中单击该球,分数就会增加一? - how can i add a mouse click to this program that will increment a score by one each time the ball is clicked in C#? 如何在C#和WinForms中将项添加到ListBox? - How can I add an item to a ListBox in C# and WinForms? 如何使用 ItemSource 向 WPF 列表框添加其他项目? - How can I add an additional item to a WPF ListBox using ItemSource? 如何识别点击的按钮属于哪个列表框项? - How to identify the clicked button belongs to which listbox item? 单击按钮时如何从列表框中获取绑定的项目 - How to get the binded Item from a listbox when a button is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM