Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add item to the beginning of the list in ListBox?

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.

like image 628
kyrisu Avatar asked May 04 '09 12:05

kyrisu


People also ask

How do I add items to my ListBox?

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.

How do I move items up and down in ListBox?

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.

How do I sort items in 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.


2 Answers

Use the Insert method on the items of your ListBox.

like image 119
bruno conde Avatar answered Sep 23 '22 16:09

bruno conde


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.

like image 28
Razzie Avatar answered Sep 22 '22 16:09

Razzie