How do I drag and drop the listview imagelist with the text?

  • Thread starter Thread starter jmDesktop
  • Start date Start date
J

jmDesktop

I have two listview controls. I have three items of text. I can drag
and drop the listview items between each other, back and forth. But
the images from the imagelist do not copy over from listview1 to
listview2, only the text does. Both listviews have their
smallimagelist as the single imagelist I have. Here is my code and
thank you for any help.

private void listView1_ItemDrag(object sender,
ItemDragEventArgs e)
{ int max = listView1.SelectedItems.Count;
ListViewItem [] myItems = new ListViewItem[max];
int i = 0;

foreach (ListViewItem myItem in listView1.SelectedItems)
{
myItems = myItem;
i+=1;
}
listView1.DoDragDrop(new
DataObject("System.Windows.Forms.ListViewItem[]", myItems),
DragDropEffects.Move);
}

private void listView2_DragEnter(object sender, DragEventArgs
e)
{
if
(e.Data.GetDataPresent("System.Windows.Forms.ListViewItem[]"))
e.Effect = DragDropEffects.Move;
else
{
e.Effect = DragDropEffects.None;
}
}

private void listView2_DragDrop(object sender, DragEventArgs
e)
{
ListView.SelectedListViewItemCollection myList
= this.listView1.SelectedItems;


int i = 0;

foreach (ListViewItem myItem in myList)
{
listView2.Items.Add(myItem.Text);
listView1.Items.Remove(listView1.SelectedItems);
i += 1;
}
}
//***
private void listView2_ItemDrag(object sender,
ItemDragEventArgs e)
{
int max = listView2.SelectedItems.Count;
ListViewItem[] myItems = new ListViewItem[max];
int i = 0;

foreach (ListViewItem myItem in listView2.SelectedItems)
{
myItems = myItem;
i += 1;
}
listView2.DoDragDrop(new
DataObject("System.Windows.Forms.ListViewItem[]", myItems),
DragDropEffects.Move);
}

private void listView1_DragEnter(object sender, DragEventArgs
e)
{
if
(e.Data.GetDataPresent("System.Windows.Forms.ListViewItem[]"))
e.Effect = DragDropEffects.Move;
else
{
e.Effect = DragDropEffects.None;
}
}

private void listView1_DragDrop(object sender, DragEventArgs
e)
{

ListView.SelectedListViewItemCollection myList=
this.listView2.SelectedItems;


int i = 0;

foreach (ListViewItem myItem in myList)
{
listView1.Items.Add(myItem.Text);
listView2.Items.Remove(listView2.SelectedItems);
i += 1;
}
}
 
I wouldn't just try and place the ListViewItem onto the clipboard.
Chances are that the image isn't being serialized to the clipboard and
that's why you aren't getting the image on the other side.

Rather, why not create a structure/class which has the information you
need (text, index in the image list) and then recreate a new ListViewItem on
the other side?
 
I amended to this line and it worked:

listView1.Items.Add(myItem.Text, myItem.ImageIndex);



I wouldn't just try and place the ListViewItem onto the clipboard.
Chances are that the image isn't being serialized to the clipboard and
that's why you aren't getting the image on the other side.

Rather, why not create a structure/class which has the information you
need (text, index in the image list) and then recreate a new ListViewItem on
the other side?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I have two listview controls. I have three items of text. I can drag
and drop the listview items between each other, back and forth. But
the images from the imagelist do not copy over from listview1 to
listview2, only the text does. Both listviews have their
smallimagelist as the single imagelist I have. Here is my code and
thank you for any help.
private void listView1_ItemDrag(object sender,
ItemDragEventArgs e)
{ int max = listView1.SelectedItems.Count;
ListViewItem [] myItems = new ListViewItem[max];
int i = 0;
foreach (ListViewItem myItem in listView1.SelectedItems)
{
myItems = myItem;
i+=1;
}
listView1.DoDragDrop(new
DataObject("System.Windows.Forms.ListViewItem[]", myItems),
DragDropEffects.Move);
}

private void listView2_DragEnter(object sender, DragEventArgs
e)
{
if
(e.Data.GetDataPresent("System.Windows.Forms.ListViewItem[]"))
e.Effect = DragDropEffects.Move;
else
{
e.Effect = DragDropEffects.None;
}
}
private void listView2_DragDrop(object sender, DragEventArgs
e)
{
ListView.SelectedListViewItemCollection myList
= this.listView1.SelectedItems;
int i = 0;
foreach (ListViewItem myItem in myList)
{
listView2.Items.Add(myItem.Text);
listView1.Items.Remove(listView1.SelectedItems);
i += 1;
}
}
//***
private void listView2_ItemDrag(object sender,
ItemDragEventArgs e)
{
int max = listView2.SelectedItems.Count;
ListViewItem[] myItems = new ListViewItem[max];
int i = 0;

foreach (ListViewItem myItem in listView2.SelectedItems)
{
myItems = myItem;
i += 1;
}
listView2.DoDragDrop(new
DataObject("System.Windows.Forms.ListViewItem[]", myItems),
DragDropEffects.Move);
}

private void listView1_DragEnter(object sender, DragEventArgs
e)
{
if
(e.Data.GetDataPresent("System.Windows.Forms.ListViewItem[]"))
e.Effect = DragDropEffects.Move;
else
{
e.Effect = DragDropEffects.None;
}
}
private void listView1_DragDrop(object sender, DragEventArgs
e)
{
ListView.SelectedListViewItemCollection myList=
this.listView2.SelectedItems;
int i = 0;
foreach (ListViewItem myItem in myList)
{
listView1.Items.Add(myItem.Text);
listView2.Items.Remove(listView2.SelectedItems);
i += 1;
}
}- Hide quoted text -


- Show quoted text -
 
Back
Top