简体   繁体   中英

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. Happens that i want add textbutton in listbox when an specified button is pressed.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM