简体   繁体   English

在树状视图中,DragEnter和DragDrop事件未触发

[英]DragEnter and DragDrop events not FIRING in treeview

I need to do drag and drop using treeview in c#.For that i heard 3 events are the most common 1.itemDrag 2.DragDrop and 3.DragEnter. 我需要在c#中使用treeview进行拖放。为此,我听到了3个事件是最常见的1.itemDrag 2.DragDrop和3.DragEnter。

whereas here itemDrag event is firing for me while dragging from a treeview ,but rest both the events are not firing for me.Tried many solutions and now came here for an solution 而这里的itemDrag事件正在从树形视图中拖动时为我触发,但其余两个事件都不为我触发。尝试了许多解决方案,现在来到这里寻求解决方案

    private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
    {
       string[] strItem = e.Item.ToString().Split(':');
       DoDragDrop(strItem[1], DragDropEffects.Copy | DragDropEffects.Move); }

the above method fires , 以上方法触发

    private void treeView1_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }

the bove dragEnter event is not firing ,similarly the dragDrop event is also not firing.Why it's so?? bove dragEnter事件没有触发,类似的dragDrop事件也没有触发。为什么呢?

Am dragging from the treeview and need to paste in PowerPoint or Word. 正在从树形视图中拖动,需要粘贴到PowerPoint或Word中。 (ie) treeview is something like an AddIn for Office Tools. (ie)treeview类似于Office工具的插件。

Regards, Arshad 问候,阿尔沙德

Allow Drop AND.. 允许删除AND ..

Ok, assuming you have all of your events being declared/created in the Form_Load... ei : 好的,假设您已在Form_Load ...中声明/创建了所有事件... ei:

tlAssemblies.DragDrop +=tlAssemblies_DragDrop;
tlAssemblies.MouseDown +=tlAssemblies_MouseDown;
tlAssemblies.MouseMove +=tlAssemblies_MouseMove;
tlAssemblies.DragEnter +=tlAssemblies_DragEnter;
tlAssemblies.DragOver += tlAssemblies_DragOver;

The drag Event is for when you fire the drag event inside your treeView which is why it is working. 当您在treeView内部触发拖动事件时,拖动事件就是针对的,这就是它起作用的原因。 The dragEnter is when you enter the boundaries of a different* control. 当您输入其他*控件的边界时,将使用dragEnter。

ie you want to drag from treeview 1 into treeview2. 即您要从树视图1拖动到树视图2中。

If you are not trying to drag the item into a different control dragEnter is wrong. 如果您不打算将项目拖到另一个控件中,dragEnter是错误的。

Here is a drag drop event sample : 这是一个拖放事件示例:

 private void tlAssemblies_DragDrop(object sender, DragEventArgs e)
 {
    if (sender == null)
        return;
    Point p = tlAssemblies.PointToClient(new Point(e.X, e.Y));
    TreeListNode dragNode = e.Data.GetData(typeof(TreeListNode)) as TreeListNode;
    TreeListNode targetNode = tlAssemblies.CalcHitInfo(p).Node;
    if (targetNode == null)
    {
        return;
    }

} Not sure if it is possible but you may want to change the dragEnter code you have and simply use it in the drag event ie 不确定是否可以,但是您可能想要更改所拥有的dragEnter代码,并在拖动事件中简单地使用它,即

  e.Effect = DragDropEffects.Move; 

If you are not leaving the same control you are dragging both to and fro, might as well show the drag movement. 如果您没有保留相同的控件,则将来回拖动都可能会显示拖动运动。

Another thing you could do is on the treeView_MouseMove Event. 您可以做的另一件事是在treeView_MouseMove事件上。

 private void tlAssemblies_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && downHitInfo != null)
        {
            Size dragSize = SystemInformation.DragSize;
            Rectangle dragRect = new Rectangle(new Point(downHitInfo.HitPoint.X -     dragSize.Width / 2,
            downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize);
            if (!dragRect.Contains(new Point(e.X, e.Y)))
            {
                DataRow row = viewProduct.GetDataRow(downHitInfo.RowHandle);
                if(row != null)
                tlAssemblies.DoDragDrop(row, DragDropEffects.Move); 
                //viewProduct.GridControl.DoDragDrop(row, DragDropEffects.All); 
                downHitInfo = null;
                DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = true;
            }
        }
    }

From your provided code, I cannot see that you have implemented the DragDrop event nor have set the AllowDrop property on the controls involved, which is needed along with the two other events in order to perform a drag and drop. 从提供的代码中,我看不到您已经实现了DragDrop事件,也没有在所涉及的控件上设置AllowDrop属性,这与其他两个事件一起需要,以便执行拖放操作。

Here is sample snatched directly from MSDN , which uses two treeviews to move nodes in between. 这是直接从MSDN抓取的示例,该示例使用两个树视图在其间移动节点。 Add a couple of treeviews, add some root and child nodes, and wire up these events. 添加几个树视图,添加一些根节点和子节点,然后连接这些事件。 Remember the AllowDrop property. 记住AllowDrop属性。

I have added a couple of Debug.WriteLine() to help with the debugging while testing this. 我添加了几个Debug.WriteLine()来帮助调试测试。 Can be hard to do with breakpoints ;-) 用断点可能很难做到;-)

NOTE: For brewity I have not supplied the event wiring code, nor the code for creating sample nodes. 注意:对于酿造性,我没有提供事件接线代码,也没有提供用于创建示例节点的代码。 If needed this can be found in the referenced article. 如果需要,可以在参考文章中找到。 Otherwise add those using the property window. 否则,使用属性窗口添加那些。

Sample code: 样例代码:

private void treeView_ItemDrag(object sender, ItemDragEventArgs e)
{
    Debug.WriteLine("ItemDrag fired!");
    DoDragDrop(e.Item, DragDropEffects.Move);
}

private void treeView_DragEnter(object sender, DragEventArgs e)
{
    Debug.WriteLine("TreeView DragEnter fired!");
    e.Effect = DragDropEffects.Move;
}

private void treeView_DragDrop(object sender, DragEventArgs e)
{
    Debug.WriteLine("TreeView DragDrop fired!");
    TreeNode NewNode;
    if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
    {
        Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
        TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(pt);
        NewNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
        if (DestinationNode.TreeView != NewNode.TreeView)
        {
            DestinationNode.Nodes.Add((TreeNode)NewNode.Clone());
            DestinationNode.Expand();
            //Remove Original Node
            NewNode.Remove();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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