Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ListView don';若项目在控件外拖放,则在右键单击时不显示关联菜单_C#_Winforms_Listview - Fatal编程技术网

C# ListView don';若项目在控件外拖放,则在右键单击时不显示关联菜单

C# ListView don';若项目在控件外拖放,则在右键单击时不显示关联菜单,c#,winforms,listview,C#,Winforms,Listview,我的列表视图显示一个上下文菜单,如果用户右键单击它。用户可以通过鼠标左/右键单击进行拖放。问题是,当用户右键单击并拖动控件外的项目时,拖动操作工作正常,但当鼠标指针返回控件(列表视图)时,它会检测鼠标向上并显示上下文菜单。我不知道怎么解决,知道吗 更新代码-万一有人想知道我在做什么,请纠正我的错误 private static bool boolMouseUpOutside=false; private void listView_MouseDown(object sender, MouseEv

我的列表视图显示一个上下文菜单,如果用户右键单击它。用户可以通过鼠标左/右键单击进行拖放。问题是,当用户右键单击并拖动控件外的项目时,拖动操作工作正常,但当鼠标指针返回控件(列表视图)时,它会检测鼠标向上并显示上下文菜单。我不知道怎么解决,知道吗

更新代码-万一有人想知道我在做什么,请纠正我的错误

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;
}
更新
我可以设置一个静态变量,并将其设置为鼠标离开事件。在启动mouse-up事件之前,我可以检查变量是否已设置。但是,如果有其他方法,请提问。如果鼠标右键单击并按住后指针离开控件,则可能正在清除鼠标向上移动事件。

这与您的解决方案没有太大区别,因为它还使用了一个标志变量,可以是类级别,也可以是位于
列表视图中的.Tag
;它只使用了正确的类型,并使用了
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;
}

你的答案比我的更准确。如果发生鼠标拖放事件,我找不到另一种方法来清除事件。所以现在,我必须说,如果存在拖放事件,这可能是解决右键单击事件的唯一方法。谢谢你的帮助。