Form has one Combobox and one ListBox. When the \"Add\" button is clicked, I want to add the selected item from the ComboBox to the ListBox.
public partial c
I would suggest to use BindingSource as it would properly update connected controls.
public partial class MyForm : Form
{
List data = new List();
BindingSource bs = new BindingSource();
public MyForm()
{
IntializeComponents();
bs.DataSource = data;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "Id";
listBox1.DataSource = bs;
}
private void buttonAddData_Click(object sender, EventArgs e)
{
var selection = (MyData)comboBox1.SelectedItem;
data.Add(selection);
bs.ResetBindings(false);
}
}
Changing controls data source on fly produces strange result sometime.