468,545 Members | 1,741 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,545 developers. It's quick & easy.

Drag and Drop multiple items

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?
Jun 15 '06 #1
3 5493
rob
On Thu, 15 Jun 2006 17:23:47 -0400, Rob <Ro*@nospam.com> wrote:
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?

Jun 16 '06 #2
rob
On Fri, 16 Jun 2006 03:05:06 -0400, rob <ro*@nospam.com> wrote:
On Thu, 15 Jun 2006 17:23:47 -0400, Rob <Ro*@nospam.com> wrote:
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();
}


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?

Jun 16 '06 #3
Rob
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);
}
}
}

Jun 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Karuppasamy | last post: by
reply views Thread by Lauren Quantrell | last post: by
2 posts views Thread by SamSpade | last post: by
3 posts views Thread by Ajay Krishnan Thampi | last post: by
1 post views Thread by emferrari | last post: by
2 posts views Thread by Dolorous Edd | last post: by
3 posts views Thread by VB Programmer | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.