Update listbox item c#
Ngày đăng:
03/01/2022
Trả lời:
0
Lượt xem:
108
To elaborate a little, because I know my answer might be a little cryptic: { public string name; public int age; public foo(string aName, int anAge) { name= aName; age = anAge; } public override string ToString() { return name; } } And you might be storing it in an array like so: private foo[] thelist; private void Form1_Load(object sender, System.EventArgs e) { thelist = new foo[20]; for (int i = 0; i < thelist.Length; i++) { thelist[i] = new foo("foo " + i, i); } listBox1.DataSource = thelist; }Alternatively, if you don't know how many you're going to be storing, you can just use an ArrayList: private ArrayList thelist; private void Form1_Load(object sender, System.EventArgs e) { thelist = new ArrayList(); for (int i = 0; i<20; i++) { thelist.Add (new foo("foo " + i, i)); } listBox1.DataSource = thelist; } Now, wherever you change one of the items in the list you force the list to refresh itself: foo tempfoo = (foo)listBox1.SelectedItem; //or any known index tempfoo.name = somenewstring; CurrencyManager cm = (CurrencyManager) BindingContext[thelist]; cm.Refresh();There may be a way to force a refresh of the list that isn't bound to another data structure, but nothing jumps out at me. |