Drag and Drop multiple items

  • Thread starter Thread starter Rob
  • Start date Start date
R

Rob

I've searched for info on how to drag and drop a group of strings (or
any other object) from one control to another. Looked through
articles by Dino Esposito, checked the Forms books by Sells and
MacDonald. You'd think it would be there.

Anyone know?
 
I've searched for info on how to drag and drop a group of strings (or
any other object) from one control to another. Looked through
articles by Dino Esposito, checked the Forms books by Sells and
MacDonald. You'd think it would be there.

Anyone know?

No takers? To qualify this, I *can* retrieve an array of files that
are selected and dropped from explorer. No problem, MSDN has an
example.

But what I need to do is to drag and drop strings or structs, and I
need to handle capturing them myself, in a ListBox or ListView. I can
handle a single string easy enough:

On sending side:

private void OnMouseDown()
{
string s = AListBox.SelectedItem.ToString();
}

On receiving side:

private void X_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}

private void X_DragDrop(object sender, DragEventArgs e)
{
string droppedString = (String)e.Data.GetData(DataFormats.Text);
}

So... How would one convert this to use string arrays rather than just
single strings?
 
No takers? To qualify this, I *can* retrieve an array of files that
are selected and dropped from explorer. No problem, MSDN has an
example.

But what I need to do is to drag and drop strings or structs, and I
need to handle capturing them myself, in a ListBox or ListView. I can
handle a single string easy enough:

On sending side:

private void OnMouseDown()
{
string s = AListBox.SelectedItem.ToString();
}

Oops...yes, I do have this line in the function above:
DoDragDrop(s, DragDropEffects.Copy);

As mentioned, this does work for single strings. I've tried
building arrays from selected objects, and tried casting the return
type to string[], etc. Something goes wrong each time. The problem
may be with the lack of data type for string[] on the receiving side.
On receiving side:

private void X_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}

private void X_DragDrop(object sender, DragEventArgs e)
{
string droppedString = (String)e.Data.GetData(DataFormats.Text);
}

So... How would one convert this to use string arrays rather than just
single strings?
 
No replies to my previous query for some reason. I'll try to simplify
this. If a form is created with two ListBoxes called 'SourceListBox'
and 'DestListBox', and the code below is hooked into drag-drop events,
this will copy single strings from one box to the other.

If the flag on the source box is set for multi-select, how would the
code need to be adapted for dragging multiple strings? (I have tried
various approaches with string[] and StringCollection...no luck)

Simplified example, works only with single strings:

namespace DragTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
SourceListBox.Items.Add("Item 1");
SourceListBox.Items.Add("Item 2");
SourceListBox.Items.Add("Item 3");
SourceListBox.Items.Add("Item 4");
SourceListBox.Items.Add("Item 5");
}

private void SourceListBox_MouseDown(object sender,
MouseEventArgs e)
{
//
// Single string
//
string s = SourceListBox.SelectedItem.ToString();
DoDragDrop(s, DragDropEffects.Copy);
}

private void TargetListBox_DragEnter(object sender,
DragEventArgs e)
{
//
// Single string
//
if (e.Data.GetDataPresent(DataFormats.StringFormat))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}

private void TargetListBox_DragDrop(object sender,
DragEventArgs e)
{
//
// Single strings
//
string droppedString =
(String)e.Data.GetData(DataFormats.Text);
TargetListBox.Items.Add(droppedString);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top