开发者

Moving items in a ListBox

I have a ListBox that has a bunch of items in it. However, I want to be able to sort the items in the list based on a match in the Item.

So if I had some items that looked like this in the ListBox:

U3      IC-00276G   236.135  198.644  90   BGA48     
U12     IC-00270G   250.610  201.594  0    SOP8      
J1      INT-00112G  269.665  179.894  180  SOIC16    
J2      INT-00112G  269.665  198.144  180  SOIC16    
FID2    FIDUCIAL    5.080    24.130   0    FIDUCIAL  
FID1    FIDUCIAL    5.080    189.818  0    FIDUCIAL 
FID3    FIDUCIAL    0        112.231  90   FIDUCIAL 

I want to be able to keep everything the same unless the item contains "FID". At which point I want to add them to the top of the ListBox and in numeric order.. so in other words the NEW ListBox would look like this:

FID1    FIDUCIAL    5.080    189.818  0    FIDUCIAL 
FID2    FIDUCIAL    5.080    24.130   0    FIDUCIAL 
FID3    FIDUCIAL    0        112.231  90   FIDUCIAL 
U3      IC-00276G   236.135  198.644  90   BGA48     
U12     IC-00270G   250.610  201.594  0    SOP8      
J1      INT-00112G  269.665  179.894  180  SOIC16    
J2      INT-00112G  269.665  198.144  180  SOIC16    

I was going about this using something like this:

if (aListBox.Items.Contains("FID"))
{
    # I don't know what to put in here to make it grab the Item and move it
    # to the top of the List and also numerically ordering them.
}

- Can anyone help with this?

Thanks in advance :)

EDIT:

foreach (var item in listOneLines)
    if (item.Contains("FID "))
        ListBox.Items.Add(item);
foreach (var item in listOneLines)
    if (item.Contains("FID0"))
        ListBox.Items.Add(item);
foreach (var item in listOneLines)
    if (item.Contains("FID1"))
        ListBox.Items.Add(item);
foreach (var item in listOneLines)
    if (item.Contains("FID2"))
        ListBox.Items.Add(item);
foreach (var item in listOneLines)
    if (item.Contains("FID3"))
        ListBox.Items.Add(item);
foreach (string item in listOneLines)
    if (!item.Contains("FID"))
        ListBox.Items.Add(item);

and the new ListBox loops like this:

FID1    FIDUCIAL    5.开发者_开发技巧080    189.818  0    FIDUCIAL 
FID2    FIDUCIAL    5.080    24.130   0    FIDUCIAL 
FID3    FIDUCIAL    0        112.231  90   FIDUCIAL 
U3      IC-00276G   236.135  198.644  90   BGA48     
U12     IC-00270G   250.610  201.594  0    SOP8      
J1      INT-00112G  269.665  179.894  180  SOIC16    
J2      INT-00112G  269.665  198.144  180  SOIC16    
FID2    FIDUCIAL    5.080    24.130   0    FIDUCIAL    #From here down is what
FID1    FIDUCIAL    5.080    189.818  0    FIDUCIAL    #I want to be removed.
FID3    FIDUCIAL    0        112.231  90   FIDUCIAL 


You can group or sort the items in many ways as you populate the listBox.
I've given some examples below. They're not necessaarily the most amazing solutions, but I figure that you'll learn more by seeing some of the possibilities than by giving a cryptic or "clever" solution. These also illustrate how you can insert at different locations in the list (as mentioned in the question).

A simple approach to separate the one list into two (unsorted) lists is by adding to the end or inserting at the beginning of the list (not a very efficient approach but often this won't be a relevant problem in UI code unless you have large lists):

foreach (string item in itemList)
{
    if (item.Contains("FID"))
        listbox.Items.Insert(0, item);  // Add at start of list
    else
        listBox.Items.Add(item);        // Add at end of list
}

(Note that the FID items will appear in a reversed order)

Or add the items in two passes:

foreach (string item in itemList)
{
    if (item.Contains("FID"))
        listbox.Items.Add(item)    // Add all items with FID in them
}
foreach (string item in itemList)
{
    if (!item.Contains("FID"))
        listbox.Items.Add(item)    // Add all items without FID in them
}

Or find an insertion location for every item:

foreach (string item in itemList)
{
    int insertPos = 0;
    bool itemIsFID = item.Contains("FID");
    while (insertPos < listBox.Items.Count)
    {
        // Primary sort - put FID items ahead of non-FID items
        bool boxItemIsFID = listBox.Items[insertPos].Contains("FID");
        if (itemIsFID && !boxItemIsFID)
        {
            // The new item must be inserted before the existing item
            break;
        }

        // Secondary sort - alphabetical
        if (item.CompareTo(listBox.Items[insertPos]) > 0)
        {
            // The new item must be inserted before the existing item
            break;
        }
    }

    // Insert the item at the location we've found
    if (insertPos < listBox.Items.Count)
        listBox.Items.Insert(insertPos, item);
    else
        listBox.Items.Add(item);
}

Or finally, you could Pre-sort a collection of the items by implementing your own IComparer and using your collection's Sort method:

itemList.Sort(MyComparer);
foreach (string item in itemList)
    listbox.Items.Add(item);


You can implement custom sort using IComparer

See this tutorial as an example or this one

Inside your custom sort method you can easily put your conditions for sorting

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜