selecting items in ListBox
-
i want to make a button on my form which will select all items in a listbox (and another button to deselect all). im not sure how to do this. also, i would like to do it with a for.. each loop, as im new to them.. not sure how to, in code, set the selecteditems (or whatever the property is, not sure which one to use).
Dim item As String For Each item In ListBox1.Items ListBox1.SelectedItem = ListBox1.Items.IndexOf(item) Next
is as far as iv got, but it doesnt seem to do anything.. any help appreciated ------------------------ Jordan. III -
i want to make a button on my form which will select all items in a listbox (and another button to deselect all). im not sure how to do this. also, i would like to do it with a for.. each loop, as im new to them.. not sure how to, in code, set the selecteditems (or whatever the property is, not sure which one to use).
Dim item As String For Each item In ListBox1.Items ListBox1.SelectedItem = ListBox1.Items.IndexOf(item) Next
is as far as iv got, but it doesnt seem to do anything.. any help appreciated ------------------------ Jordan. IIIFirst, ensure that your listbox's
SelectionMode
property is set to MultiSimple or MultiExtended. Next, ListBox has a method namedSetSelected
which is called with 2 arguments (index and boolean "Selected") So to select all items you could use this code:for(int i=0;i<ListBox1.Items.Count;i++) { ListBox1.SetSelected(i,true); }
-
First, ensure that your listbox's
SelectionMode
property is set to MultiSimple or MultiExtended. Next, ListBox has a method namedSetSelected
which is called with 2 arguments (index and boolean "Selected") So to select all items you could use this code:for(int i=0;i<ListBox1.Items.Count;i++) { ListBox1.SetSelected(i,true); }