I have loaded some images in a listview using imagelist in c#. Now I want to copy any image and replace into another image in the same listview using drag and drop. How could I do this? sample code would be helpful.
Thanks in advance.
I have loaded some images in a listview using imagelist in c#. Now I want to copy any image and replace into another image in the same listview using drag and drop. How could I do this? sample code would be helpful.
Thanks in advance.
Do you mean drag an image file into the listview? Or drag an item within the same listview? Or drag from one listview to another control?
Do you mean drag an image file into the listview? Or drag an item within the same listview? Or drag from one listview to another control?
"Drag an item within the same listview".
This is a pretty good place to start. This is for reordering items in a listview though, so you will have to make some changes to get the items to replace on drag.
I put this together to learn how to do this myself. It's not exactly trivial, as it turns out. This is quite a bit more specific to your actual problem than that link I provided.
private void listView1_DragDrop(object sender, DragEventArgs e)
{
//Translate the mouse coordinates (screen coords) into control coordinates
Point p = listView1.PointToClient(new Point(e.X, e.Y));
//Find the item that the object was dragged onto
var ItemToReplace = listView1.GetItemAt(p.X, p.Y);
//extract the listview item from the dragged items IData member
var DraggedItem = ((ListViewItem)e.Data.GetData(typeof(ListViewItem)));
//If the item isn't from THIS listview, dont allow the drop
if (DraggedItem.ListView != listView1)
return;
//Make sure that the user dragged over a valid item
if (ItemToReplace != null)
{
//Get the index of the item that was dragged over
int ReplaceIndex = listView1.Items.IndexOf(ItemToReplace);
//To just copy a property (like an image or text)
listView1.Items[ReplaceIndex].Text = DraggedItem.Text;
//To copy a complete clone of the item
//Removes the item that is to be replaced
//listView1.Items[ReplaceIndex].Remove;
//Creates a clone of the dragged item object and inserts it in the replaced items index
//listView1.Items.Insert(ReplaceIndex, (ListViewItem)DraggedItem.Clone()); //
}
}
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
//Start the dragdrop operation with the currently dragged listview item as the drag data
listView1.DoDragDrop((ListViewItem)e.Item, DragDropEffects.Move);
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
//If the item being dragged isn't associated with this listview, it's not allowed to be dragged here
if (((ListViewItem)e.Data.GetData(typeof(ListViewItem))).ListView == listView1)
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
Thank you. It is exactly that I wanted.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.