为什么拖放在我的鼠标按下右键单击上不起作用

本文关键字:右键 单击 不起作用 拖放 我的 鼠标 为什么 | 更新日期: 2023-09-27 17:57:24

以下代码拒绝在鼠标右键单击时进行拖放。 当我右键单击鼠标时,我确实看到了正确的上下文菜单,但我无法拖放,尽管我确实有一个用于 DragDrop 、DragEnter 和 DragOver 的事件处理程序。是因为我不能在同一次右键单击上拥有上下文菜单和拖放吗? 我做错了什么? 非常感谢您的帮助。

private void treeList1_MouseDown(object sender, MouseEventArgs e)
{
    TreeList tree = sender as TreeList;
    Point pt = tree.PointToClient(MousePosition);
    TreeListHitInfo info = tree.CalcHitInfo(pt);
    if (e.Button == MouseButtons.Right && ModifierKeys == Keys.None && tree.State == TreeListState.Regular)
    {
        if (nodeType == typeof(X))
        {
            tree.ContextMenuStrip = XContextMenu;
            tree.FocusedNode = info.Node;
            treeList1.AllowDrop = true;
            tree.AllowDrop = true;
        }
        currentFocusNode = tree.FocusedNode;
        return;
    }
}

为什么拖放在我的鼠标按下右键单击上不起作用

您没有调用 DoDragDrop 方法。

以下是使用拖放的示例

在您的示例中,在return;之前添加类似以下内容

的内容
treeList1.DoDragDrop(currentFocusNode, DragDropEffects.Copy);
这是

如何执行拖放到列表视图的示例:

private void Form1_Load(object sender, EventArgs e)
{
    listView1.AllowDrop = true;
    listView1.DragDrop += new DragEventHandler(listView1_DragDrop);
    listView1.DragEnter += new DragEventHandler(listView1_DragEnter);
}
void listView1_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Copy;
}
void listView1_DragDrop(object sender, DragEventArgs e)
{
    listView1.Items.Add(e.Data.ToString());
}