Results 1 to 7 of 7

Thread: [1.0/1.1] Moving the selected items of a listbox UP or DOWN [C# 2002]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

    Question [1.0/1.1] Moving the selected items of a listbox UP or DOWN [C# 2002]

    Given a form (frmMain) which contains a listbox "lbSelected" (multi-select) and 2 control buttons "btnMoveUp" & "btnMoveDown" that should allow the user to move the selected items (from the listbox) up or down using the buttons. Thing is I have no clue how to change the index of a selected item in a listbox, forget trying to handle multi-select - so I was hoping maybe someone could give me some help/hints.

    So far I got to this point:
    Code:
    // Get the current index of all selected items in the listbox
    ListBox.SelectedIndexCollection indxcolSelected = lbSelected.SelectedIndices;
    So this will give me the index of each of the selected items...

    Consequently it could also be good to do it this way I imagine:
    Code:
    ListBox.SelectedObjectCollection objcolSelected = lbSelected.SelectedItems;
    Where I am getting the list of selected items instead and somehow modifiying their index?

    Now I need to find a way to either +1 or -1 each of the corresponding items indexes (depending on if I am going UP or DOWN) and always be careful that I am not already at the top or bottom...

    Any help would be greatly appreciated.
    Thanks,

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [1.0/1.1] Moving the selected items of a listbox UP or DOWN [C# 2002]

    Hi there,
    I'm unsure about the multi-select at the moment, but as for the single selected items, I'm not sure if this is best but perhaps something like this?
    Code:
                // Up
                int i = this.listBox1.SelectedIndex;
                object o = this.listBox1.SelectedItem;
    
                if (i > 0)
                {
                    this.listBox1.Items.RemoveAt(i);
                    this.listBox1.Items.Insert(i - 1, o);
                    this.listBox1.SelectedIndex = i - 1;
                }
    
    
                // Down
                int i = this.listBox1.SelectedIndex;
                object o = this.listBox1.SelectedItem;
    
                if (i < this.listBox1.Items.Count - 1)
                {
                    this.listBox1.Items.RemoveAt(i);
                    this.listBox1.Items.Insert(i + 1, o);
                    this.listBox1.SelectedIndex = i + 1;
                }

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

    Re: [1.0/1.1] Moving the selected items of a listbox UP or DOWN [C# 2002]

    Ya that is what I was thinking about trying next but I thought something more user friendly then re-mapping the entire listbox would be possible...
    I can only imagine how nasty that would be if there are like 100 items and the objects actually have content (are not just strings or ints for example).

    But thanks - worst case I'll use a model of your code (for multi-select).
    Much appreciated.

  4. #4
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [1.0/1.1] Moving the selected items of a listbox UP or DOWN [C# 2002]

    Hi Shaitan,

    Did you come up with anything else for this? I played around with the multi-select a little bit and got this. I imagine there is a better way. Post again when you find it.

    Code:
                ListBox.SelectedIndexCollection col = this.listBox1.SelectedIndices;
    
                for (int i = this.listBox1.Items.Count - 1; i >= 0; i--)
                {
                    if ((col.Contains(i)) && (i < this.listBox1.Items.Count - 1))
                    {
                        object o = this.listBox1.Items[i];
                        this.listBox1.Items.RemoveAt(i);
                        this.listBox1.Items.Insert(i + 1, o);
                        this.listBox1.SetSelected(i + 1, true);
                    }                
                }

  5. #5
    Registered User RaviIntegra's Avatar
    Join Date
    Mar 2007
    Location
    Pondicherry, India
    Posts
    125

    Re: [1.0/1.1] Moving the selected items of a listbox UP or DOWN [C# 2002]

    vb Code:
    1. private void btnUp_Click(object sender, EventArgs e)
    2.         {
    3.             int ilevel=0;
    4.             if (listView1.SelectedItems.Count > 0)
    5.             {
    6.                 for (int itmp = 0; itmp<=listView1.Items.Count - 1; itmp++)
    7.                 {
    8.                     if (listView1.Items[itmp].Selected == true)
    9.                     {
    10.                         ilevel = itmp;
    11.                         if (ilevel - 1 >= 0)
    12.                         {
    13.                             ListViewItem lvitem = listView1.Items[itmp];
    14.                             listView1.Items.Remove(lvitem);
    15.                             listView1.Items.Insert(ilevel - 1, lvitem);
    16.                         }
    17.  
    18.                     }
    19.                 }
    20.             }        
    21.         }
    22.  
    23.         private void btnDown_Click(object sender, EventArgs e)
    24.         {
    25.             int iLevel = 0;
    26.             if (listView1.SelectedItems.Count > 0)
    27.             {
    28.                 for (int iTmp = 0; iTmp<=listView1.Items.Count-1; iTmp++)
    29.                 {
    30.                     if (listView1.Items[iTmp].Selected == true)
    31.                     {
    32.                         iLevel = iTmp;
    33.                         if (iLevel + 1 <= listView1.Items.Count - 1)
    34.                         {
    35.                             ListViewItem lvItem = listView1.Items[iTmp];
    36.                             listView1.Items.Remove(lvItem);
    37.                             listView1.Items.Insert(iLevel + 1, lvItem);
    38.                         }
    39.                     }
    40.                 }
    41.             }        
    42.         }
    Try this code

  6. #6
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [1.0/1.1] Moving the selected items of a listbox UP or DOWN [C# 2002]

    That's basically the same thing: looping through all of the indices and checking if they are selected. However, I don't know what the .Selected property is for a ListBox (or ListView as you posted). Is that a property of a WebForm control?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

    Re: [1.0/1.1] Moving the selected items of a listbox UP or DOWN [C# 2002]

    nmadd: .Selected property is for the WebForm control as you suspected - mind you it can be done for WindowsForm controls by doing something like:
    if (listBox1.Items[nTmp] == listBox1.SelectedItem)

    BTW - your Multi-Select MoveDown works like a charm, going to move it around to get it to work for MoveUp

    Code:
    // Move Selected item up
    listbox.BeginUpdate();
    ListBox.SelectedIndexCollection col = this.listbox.SelectedIndices;
    for (int i = 0; i <= this.listbox.Items.Count - 1; i++)
    {
    	if ((col.Contains(i)) && (i > 0))
    	{
    		object o = this.listbox.Items[i];
    		this.listbox.Items.RemoveAt(i);
    		this.listbox.Items.Insert(i - 1, o);
    		this.listbox.SetSelected(i - 1, true);
    	}                
    }
    listbox.EndUpdate();

    RaviIntegra: Thanks for the post... Much appreciated.
    Sadly it won't work that easily for Multi-Select as the .Selected will have issues - I am pretty sure I need to use the same approach as nmadd by using the "SelectedIndexCollection", unless I am missing something ...
    Last edited by Shaitan00; Jul 24th, 2007 at 10:57 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width