I'm fairly new to Visual C# and I'm trying to create a List<String>
whose contents are shown by a form widget, preferably using the form editor. Coming from a Qt/C++ background I usually do something like this:
QList<string>
and derives from the Subject
class in the Observer pattern.Observer
(which again is part of the Observer pattern) and one of Qt's list widgets. This newly created widget should be able to update the list when it receives a notification from the data structure.This procedure is a pain in the butt and I'm sure that there's a better way, but I'm not here for Qt help right now. What's the quickest way to display the contents of a List<String>
(or similar structure) in C#? I'm using WinForms.
// simple one-way, one-time binding
var myItems = new List<string> { "aaa", "bbb" };
listBox1.DataSource = myItems;
// rebinding
var myItems = new List<string> { "aaa", "bbb" };
listBox1.DataSource = myItems;
....
myItems.Add("ccc");
listBox1.DataSource = myItems;
// one-way, multi-time binding
var myItems = new BindingList<string> { "aaa", "bbb" };
listBox1.DataSource = myItems;
...
myItems.Add("ccc");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With