Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting ListItems

Status
Not open for further replies.

Alcar

Programmer
Sep 10, 2001
595
US
sorry all but my mind today is somewhere else. I can't find a solution for this (simple) problem.

- given the listitems text are names
- having 2 ListBoxes and 4 buttons to manage the items from one to the other list, I realize that once I move the first 2 names from the first list (list1) and put them to the second one (list2) and then put them back into list1, the are (of course) added to the end of the list.
I need to have both lists sorted at all time.
Funny enough that it seems that the ListView (winforms) control has the capability to sort, while it's "cousin" ListBox doesn't.
I was trying to solve this problem with the SortedItem object, but I found myself short of index, text and values properties (don't ask me how that happend lol)

While I crack open my head and get fresh air, I thank you all in advance for paying attention to this problem.
Daren J. Lahey
Just another computer guy...
 
here is how I solved it (with a fresher mind hehehe)

private void SortListBox(ListBox myList)
{
string[] strList2 = new string[(myList.Items.Count)];
for(int i =0; i <=myList.Items.Count-1;i++)
{
strList2 = myList.Items.Text;
}
Array.Sort(strList2);
myList.Items.Clear();
for(int i=0; i<=strList2.Length-1;i++)
{
ListItem oItem = new ListItem(strList2.ToString(),strList2.ToString());
myList.Items.Add(oItem);
}
}

The only problem with this is that the Array.Sort(Array) method will not accept multi-dimensional arrays.
Therefore after I will have to find the ID or index to the selected elements of the listbox with another query to the DB. Oh Well...

hth Daren J. Lahey
Just another computer guy...
 
make that:

private void SortListBox(ListBox myList)
{
string[] strList2 = new string[(myList.Items.Count)];
for(int i =0; i <=myList.Items.Count-1;i++)
{
strList2[ i] = myList.Items[ i].Text;
}
Array.Sort(strList2);
myList.Items.Clear();
for(int i=0; i<=strList2.Length-1;i++)
{
ListItem oItem = new ListItem(strList2[ i].ToString(),strList2[ i].ToString());
myList.Items.Add(oItem);
}
}

watch out for those [ i]!!

Daren J. Lahey
Just another computer guy...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top