How to make the Items values in the ListBox fill up from an array?
2 answers
You can use the AddRange method as an example:
string [] myArr = new string[4]; myArr[0] = "One"; myArr[1] = "Two"; myArr[2] = "Three"; myArr[3] = "Four"; myListBox.Items.AddRange(myArr); So you can immediately fill in if necessary (immediately in the sheet):
myListBox.Items.AddRange(new object[]{"One", "Two", "Three", "Four", "Five", "Six"}); If it fails, then loop through the array through the loop and add to the list:
string [] myArr = new string[4]; myArr[0] = "One"; myArr[1] = "Two"; myArr[2] = "Three"; myArr[3] = "Four"; myListBox.Items.Clear(); for (int i = 0; i < myArr.Length; i++) { myListBox.Items.Add(myArr[i].ToString()); } Use the ListBox.DataSource property to display the elements, and BindingList<T> instead of an array, if you want the changes to be displayed in the ListBox without additional shamanism.
var source = new BindingList<string>() { "One", "Two", "Three", "Four" } listBox1.DataSource = source; Now any lines added to the source will be added to the ListBox . If the array consists of composite objects or structures, you can specify the value of which property should be displayed using the ListBox.DisplayMember property.
Of course, you can also
var source = new string [] { "One", "Two", "Three", "Four" } listBox1.DataSource = source; Only in this case, to display the changes, you will have to do something like this:
listBox1.DataSource = null; listBox1.Items.Clear(); source[0] = "NewValue"; listBox1.DataSource = source; Or, as suggested earlier, in the manual editing mode.
ListBox.Items.Addover the array by adding to the sheet viaListBox.Items.Addnot tried? - Alexey Shimansky