Reputation: 1120
How can i add the data to listbox1 by pressing the Save button.I have done the code of listbox1 but not of the button.Below is the code
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
listBox1.Items.Add(textBox2.Text);
listBox1.Items.Add(textBox3.Text);
listBox1.Items.Add(textBox4.Text);
}
}
}
Upvotes: 0
Views: 4898
Reputation: 155
Delete the listBox1_SelectedIndexChanged
event and put the content to the button1_Click
event.
It should look like this:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
listBox1.Items.Add(textBox2.Text);
listBox1.Items.Add(textBox3.Text);
listBox1.Items.Add(textBox4.Text);
}
}
}
And don't forget to remove the listBox1_SelectedIndexChanged
event from the ListBox in the UI editor too.
Upvotes: 3
Reputation: 3509
If you want on button click the same thing to happen as on SelectedIndexChanged(), you just copy the code.
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
listBox1.Items.Add(textBox2.Text);
listBox1.Items.Add(textBox3.Text);
listBox1.Items.Add(textBox4.Text);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
/*listBox1.Items.Add(textBox1.Text);
listBox1.Items.Add(textBox2.Text);
listBox1.Items.Add(textBox3.Text);
listBox1.Items.Add(textBox4.Text);*/
}
If this is not it, you have to give more information about what should happen.
Upvotes: 1