简体   繁体   中英

ListView don't show context menu on right click if item is dragged and dropped outside of the control

My List View shows a context menu, if user has right clicked on it. The user can drag and drop on left/right mouse click. The problem is, when user right clicks and drags an item outside of the control, the drag operation works just fine, but when mouse pointer returns back on control (list view) it detect mouse up and shows the context menu. I have no idea how to resolve it, any idea ?

Updated Code - Just In case some one wants to know what I'm doing, please correct me if i'm wrong.

private static bool boolMouseUpOutside=false;
private void listView_MouseDown(object sender, MouseEventArgs e)
{
    boolMouseUpOutside = false;
}
private void listView_MouseUp(object sender, MouseEventArgs e)
{
    if (!boolMouseUpOutside)
    {
        ListView listview = sender as ListView;
        if (e.Button == MouseButtons.Right)
        {
            if (listview.SelectedItems.Count == 0 && !_TreeViewHasSelectedFavorites)
            {
                ListViewContextMenuHideOnNoSelection();
                listViewMenuStrip.Show(listview, e.X, e.Y);
            }
            else if (listview.SelectedItems.Count == 0 && _TreeViewHasSelectedFavorites)
            {
                ListViewContextMenuHideOnFavoritesSelection();
                listViewMenuStrip.Show(listview, e.X, e.Y);
            }
            else if (listview.SelectedItems.Count > 0)
            {
                 Object obj = listview.SelectedItems[0].Tag;
                 if (obj.GetType() == typeof(DirectoryInfo))
                 {
                      ListViewContextMenuHideOnFolderSelection();
                      listViewMenuStrip.Show(listview, e.X, e.Y);
                 }
                 if (obj.GetType() == typeof(FileInfo))
                 {
                      ListViewContextMenuHideOnFileSelection();
                      listViewMenuStrip.Show(listview, e.X, e.Y);
                 }
            }
        }
    }
}
private void listView_MouseLeave(object sender, EventArgs e)
{
    boolMouseUpOutside = true;
}

Update I can set a static variable, and set it on mouse leave event. Before firing the mouse up event, I can check to see if the variable is set or not. But asking the question, if there is another approach. May be clearing the mouse up event if the pointer has left the control after right click and hold.

This is not very different from your solution as it also uses a flag variable, either class level or maybe sitting in the ListView.Tag ; it just uses the right types and makes use of the MouseEventArgs :

MouseButtons btns = MouseButtons.None;

private void listView1_MouseDown(object sender, MouseEventArgs e)
{
    btns = Control.MouseButtons;
}

private void listView1_MouseEnter(object sender, EventArgs e)
{
    btns = Control.MouseButtons;
}

private void listView1_MouseUp(object sender, MouseEventArgs e)
{
    if (btns != e.Button) return;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM