I’m trying to make a program that allows to drag and drop files to a ListView. I use ListView.InsertionMark to identify where to insert file. When I drop the file, the InsertionMark doesn’t disappear because the DragLeave event isn’t triggered; if I change drag drop effect to DragDropEffects.None, the DragLeave event is triggered when I release the mouse. Why is this happening?
Also, the document says “If there is a change in the keyboard or mouse button state, the QueryContinueDrag event is raised and determines whether to continue the drag, to drop the data, or to cancel the operation based on the value of the Action property of the event’s QueryContinueDragEventArgs.” But the QueryContinueDrag event is also not triggered.
public partial class MainForm : Form
{
private ListView listView1 = new ListView();
public MainForm()
{
InitializeComponent();
this.Controls.Add(listView1);
listView1.Dock = DockStyle.Fill;
listView1.View = View.Details;
listView1.Columns.Add("Test");
listView1.Items.Add("0");
listView1.AllowDrop = true;
listView1.DragEnter += listView1_DragEnter;
listView1.DragOver += listView1_DragOver;
listView1.DragLeave += listView1_DragLeave;
listView1.DragDrop += listView1_DragDrop;
listView1.GiveFeedback += listView1_GiveFeedback;
listView1.QueryContinueDrag += listView1_QueryContinueDrag;
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link | DragDropEffects.Scroll;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void listView1_DragOver(object sender, DragEventArgs e)
{
// This is not done, only to show this problem.
listView1.InsertionMark.Index = 0;
listView1.InsertionMark.AppearsAfterItem = true;
}
private void listView1_DragLeave(object sender, EventArgs e)
{
listView1.InsertionMark.Index = -1;
}
private void listView1_DragDrop(object sender, DragEventArgs e)
{
// listView1.InsertionMark.Index = -1; // Is this really necessary?
}
private void listView1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
this.Text = "listView1_QueryContinueDrag";
}
private void listView1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
this.Text = "listView1_GiveFeedback";
}
}
Do I have to set listView1.InsertionMark.Index = -1 manually to hide the InsertionMark? Is there anyway to trigger the DragLeave event when drag drop effect is DragDropEffects.Link?
DragLeave:(Emphasis added)
They did neither – they completed the drop. Why are you so surprised that this event didn’t fire then?
Re:
QueryContinueDrag:(Emphasis added)
I could be wrong here, but in this instance, you appear to be the drag target, not the source.