Is there a way to add item to a WinForms ListBox, to the beginning of the list without rewriting entire list in a loop?
Other way to solve my problem would be to display ListBox in reverse order (last item on the top) but I don't know how to do it.
My ListBox control is used as a log viewer where the most recent entry should be on the top.
To add items to a ListBox, select the ListBox control and get to the properties window, for the properties of this control. Click the ellipses (...) button next to the Items property. This opens the String Collection Editor dialog box, where you can enter the values one at a line.
This is the server side implementation and the event handler of the Up\Down button will invoke the MoveUp or MoveDown method on the listbox.
Use the Sorted property to automatically sort strings alphabetically in a ListBox. As items are added to a sorted ListBox, the items are moved to the appropriate location in the sorted list. When adding items to a ListBox, it is more efficient to sort the items first and then add new items.
Use the Insert
method on the items of your ListBox
.
If I understand correctly, can't you use the Insert(int index, object item)
method? For example:
myListBox.Items.Insert(0, "First");
This inserts "First" as the first item of the listbox.
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