ComBox如何同步数据源
用List<string>作为ComBox的数据源,当List<string>发生改变时如何同步ComBox?
using System.Windows.Forms; using namespace Test { class Test { static void Main() { BindingList<string> list = new BindingList<string>(); ListBox listBox1 = new ListBox { DataSource = list, Dock = DockStyle.Left }; ListBox listBox2 = new ListBox { DataSource = list, Dock = DockStyle.Right }; foreach (var lb in new[] { listBox1, listBox2 }) { lb.KeyDown += (s, a) => { if (a.KeyCode == Keys.Delete) { list.Remove((s as ListBox).SelectedItem as string); } }; } TextBox input = new TextBox { Dock = DockStyle.Top }; input.KeyDown += (s, a) => { if (a.KeyCode == Keys.Enter) { list.Add(input.Text); } }; Form form = new Form(); form.Controls.Add(input); form.Controls.Add(listBox1); form.Controls.Add(listBox2); Application.Run(form); } } }