Drag & Drop "A first chance exeption of type 'System.ArugumentExc.

G

Guest

I am emplementing Drag & Drop in a VS2005 C# WinForms application. I have 2
ListView controls on a form, whereby I'm attempting to Drag a ListViewItem
from ListView "A" onto ListView "B."

All the events (ListView_ItemDrag, ListView_DragEnter, ListView_DragDrop,
etc., etc.) seem to be firing correctly.

My goal is to perform a Move from control A to control B. When adding the
ListViewItem to the collection on Control B we receive the following
exception:
"A first chance exception of type 'System.ArgumentException' occurred in
System.Windows.Forms.dll"

I'm taking an existing ListViewItem from control A and moving it to control
B. After a bunch of experimentation I have simply concluded that using an
existing object, for reasons I can't explain, causes the problem. Whereas,
simply adding a new ListViewItem to Control B works perfectly fine.

Checking the documentation, my implementation should work ....
ListView.Items.Add( existing ListViewItem ); This resides in the DropDrop
event handler. The existing ListViewItem is what was sent in the
ListView_ItemDrag event handler, using the DoDragDrop() method .... per the
documentation in VS2005.

Any ideas why this execption is being thrown or what it means???
 
K

Kevin Spencer

Is Control B DataBound by any chance?

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.
 
G

Guest

At this point, "no." But, it is my intention to data bind all my ListView
controls in the future. Frankly, I've been "playing" with the ListView
because I wasn't sure it would do the things I needed ... I thought I was
going to have to create a new control from scratch. I must admit I'm
impressed with how easily configurable it has been so far, and it does give
me the L&F I need.

At this point I simply have some test data - ListViewItems (some with
subitems, some with picture index values, and some with just the Text) - that
I create in the forms constructor then add them using the
ListView.Items.AddRange() method.

I quick theory: could the exception be based on the fact that ListView A
still has the dragged ListViewItems contained within its collection when I'm
trying to add them to ListView B's collection (based on event sequencing)??
Here is my existing code:

protected virtual void ListView_ItemDrag(object sender, ItemDragEventArgs e)
{
// Step 1 - ensure the Left Mouse button is down.

if (e.Button != MouseButtons.Left) return;

// Step 2 - The supported drag effects - we only support Move.

DragDropEffects supportedEffects = DragDropEffects.Move;

// Step 3 - grab the data which is to be dragged.

ListView.SelectedListViewItemCollection dragData =
this.ListView.SelectedItems;

// Step 4 - Here is where we begin the drag-&-drop operation.

DragDropEffects dragEffect = this.ListView.DoDragDrop(dragData,
supportedEffects);

// Step 5 - Check to see what the Effect was from #4. If Move,
then remove the ListViewItem from here.

if (dragEffect == DragDropEffects.Move)
{
foreach (ListViewItem selectedTask in
this.ListView.SelectedItems)
{
this.ListView.Items.Remove(selectedTask);
}
}
}


protected virtual void ListView_DragDrop(object sender, DragEventArgs e)
{
// Here is where we handle processing the actual Drop from the
user.

// Step 1 - Don't Drop if attempt is to a non-droppable location.

Point currentPosition = this.PointToClient(Cursor.Position);
if (!this.ClientRectangle.Contains(currentPosition)) return;

// Step 2 - If we're here, then we're correctly draging from
Control A to Control B, and dropping
// in a good location. So, go ahead and get the data
that the user is trying to move.

ListView.SelectedListViewItemCollection dragData =

(ListView.SelectedListViewItemCollection)e.Data.GetData(typeof(ListView.SelectedListViewItemCollection));

// Step 3 - Show the Move to the drop target (add the data).

foreach (ListViewItem task in dragData)
{
this.ListView.Items.Add(task);
}
}


It's that last statement ... Item.Add(task) ... that we throw the exception.
I've seen documentation from several sources, including MSDN, that show this
as the correct technique. Admittedly, the only difference between what I've
seen and my implementation is the fact that I'm performing DnD between 2
controls of the same type. All the other examples I've seen show it between
2 different controls, such as between a Label and a TextField, or a TreeView
and a ListView.

If I change the code to: "this.ListView.Items.Add(task.Text,
task.ImageIndex);" then everything works okay and no exception is thrown.
Apparently whatever the problem is, it's subtle. Trying to add the
ListViewItem directly to ListView control B causes a problem is that same
ListViewItem is part of the "Items" collection of ListView control A.

Any thoughts on this??? Appreciate any help you can offer.
 
J

JP

A ListViewItem may not belong to more than one ListView at the same
time.
Remove the item from the original list, then add to the new one.
Better yet, simply recreate the Item in the new list, then remove from
the old.
 
G

Guest

Thanks JP. That's kind of where I've ended up on this. I didn't know for
sure if the ListView had that restriction because I haven't found any
documentation to that extent, but our observations supported that conclusion
..... I just hate to work that way, though, because I'm new to .NET & C#, and
usually it's something I'm doing wrong versus a limitation within the
framework. I always prefer to see it in the documentation or hear about it
from an expert. Nothing quit as fun as diving into the deep end of the pool,
if you know what I mean.

Again, thanks.
 

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

Top