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
Alternatively and probably the most correct way to implement this is to use the provided ObservableCollectionINotifyCollectionChanged.
public partial class MyForm : Form
{
ObservableCollection data = new ObservableCollection();
public MyForm()
{
listBox1.DataSource = data;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "Id";
}
private void buttonAddData_Click(object sender, EventArgs e)
{
var selection = (MyData)comboBox1.SelectedItem;
data.Add(selection);
}
}
Because ObservableCollection implements INotifyCollectionChanged the DataSource binding will automatically update the ListBox whenever your data changes.