Drag & Drop in a List Box

  • Thread starter Thread starter Bruce
  • Start date Start date
B

Bruce

Is there a way I can use drag and drop within a single
list box? So that when you move an item from the list, it
will actually move it to the new position as oppose to
just copying it?
 
Hi Bruce,

Do you mean changing the orderof the items in the listbox by mouse?
If so, you may try handle the MouseDown /MouseUp event,
Save the selectedindex in the MouseDown event,
When the mouseUp event fired,
1.check the savedIndex is valid and is not same as current selection.
2. swap the two items.like
<code>
private void listBox1_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(selIdx != listBox1.SelectedIndex)
{
object temp = listBox1.Items[selIdx];
listBox1.Items[selIdx] = listBox1.SelectedItem;
listBox1.Items[listBox1.SelectedIndex] = temp;
}
selIdx = -1;
}
</code>

Does it solve your problem?





Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
Hello Ying-Shen Yu,

I appreciate your assistance. I tried what you suggested
and it worked in the sense that I could swap two items
within a list box. I would also like the ability to select
an item from the list, and drag it to a new position. For
example, take the first item in the list and drag it to
the middle of the list, thereby changing the order of the
lists contents (without swapping two items). If you have
any quick ideas I will gladly give them a try. Thanks.

Bruce
 
Hi Bruce,

Sorry, after a second read on your original post, I found I misunderstood
your meaning. now this might be what you want,
replace the code in Mouse_Up event with the code below,
If you have any problem or idea on this issue, please reply this thread to
let me know.
Thanks!

<code>
if(selIdx != -1)
{
object item = listBox1.Items[selIdx];
listBox1.BeginUpdate();
if(selIdx < listBox1.SelectedIndex )
{
listBox1.Items.Insert(listBox1.SelectedIndex + 1,item);
listBox1.Items.RemoveAt (selIdx);
listBox1.SelectedIndex += 1;
}
else if(selIdx > listBox1.SelectedIndex )
{
listBox1.Items.Insert(listBox1.SelectedIndex,item);
listBox1.Items.RemoveAt (selIdx + 1);
listBox1.SelectedIndex -= 1;
}
listBox1.EndUpdate();
selIdx = -1;
}
</code>

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
Hi Ying-Shen Yu,

Thank you very much. That is pretty much exactly what I
was looking for. I really appreciate your assistance.

Bruce
-----Original Message-----
Hi Bruce,

Sorry, after a second read on your original post, I found I misunderstood
your meaning. now this might be what you want,
replace the code in Mouse_Up event with the code below,
If you have any problem or idea on this issue, please reply this thread to
let me know.
Thanks!

<code>
if(selIdx != -1)
{
object item = listBox1.Items[selIdx];
listBox1.BeginUpdate();
if(selIdx < listBox1.SelectedIndex )
{
listBox1.Items.Insert
(listBox1.SelectedIndex + 1,item);
listBox1.Items.RemoveAt (selIdx);
listBox1.SelectedIndex += 1;
}
else if(selIdx > listBox1.SelectedIndex )
{
listBox1.Items.Insert (listBox1.SelectedIndex,item);
listBox1.Items.RemoveAt (selIdx + 1);
listBox1.SelectedIndex -= 1;
}
listBox1.EndUpdate();
selIdx = -1;
}
</code>

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.


.
 
Back
Top