PDA

Click to See Complete Forum and Search --> : [1.0/1.1] Moving the selected items of a listbox UP or DOWN [C# 2002]


Shaitan00
Jul 23rd, 2007, 12:17 PM
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:

// 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:

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,

nmadd
Jul 23rd, 2007, 12:36 PM
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?
// 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;
}

Shaitan00
Jul 23rd, 2007, 01:07 PM
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.

nmadd
Jul 23rd, 2007, 08:06 PM
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. :bigyello:

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);
}
}

RaviIntegra
Jul 24th, 2007, 01:25 AM
private void btnUp_Click(object sender, EventArgs e)
{
int ilevel=0;
if (listView1.SelectedItems.Count > 0)
{
for (int itmp = 0; itmp<=listView1.Items.Count - 1; itmp++)
{
if (listView1.Items[itmp].Selected == true)
{
ilevel = itmp;
if (ilevel - 1 >= 0)
{
ListViewItem lvitem = listView1.Items[itmp];
listView1.Items.Remove(lvitem);
listView1.Items.Insert(ilevel - 1, lvitem);
}

}
}
}
}

private void btnDown_Click(object sender, EventArgs e)
{
int iLevel = 0;
if (listView1.SelectedItems.Count > 0)
{
for (int iTmp = 0; iTmp<=listView1.Items.Count-1; iTmp++)
{
if (listView1.Items[iTmp].Selected == true)
{
iLevel = iTmp;
if (iLevel + 1 <= listView1.Items.Count - 1)
{
ListViewItem lvItem = listView1.Items[iTmp];
listView1.Items.Remove(lvItem);
listView1.Items.Insert(iLevel + 1, lvItem);
}
}
}
}
}

Try this code

nmadd
Jul 24th, 2007, 09:19 AM
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?

Shaitan00
Jul 24th, 2007, 10:48 AM
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


// 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 ...