无法将treenode移动到c#中的列表框

时间:2009-11-03 00:02:23

标签: c# winforms drag-and-drop

我在将树视图中的treenode移动到列表框时遇到问题。代码很简单。我只是想不通为什么。这是我的代码:

在窗体的构造函数中,我有:

this.ScriptTestTreeView.ItemDrag += new ItemDragEventHandler(ScriptTestTreeView_ItemDrag);

this.ActiveScriptListBox.DragEnter += new DragEventHandler(ActiveScriptListBox_DragEnter);

然后处理程序函数:

private void ScriptTestTreeView_ItemDrag(object sender, ItemDragEventArgs e)
{
    //MessageBox.Show("drag occur.");
    ScriptTestTreeView.DoDragDrop(e.Item, DragDropEffects.Link);
}

void ActiveScriptListBox_DragEnter(object sender, DragEventArgs e)
{
    //throw new NotImplementedException();
    MessageBox.Show("drag enter!"+e.Data.GetData(DataFormats.StringFormat));
}

我注意到当我尝试从树视图中拖动节点时,即使我将树视图的允许值设置为true,也会出现禁止标志。

当我将鼠标移动到列表框时,会显示消息框,但整个程序会冻结。

有没有办法调试这个问题?我在这里做错了吗?

感谢。

2 个答案:

答案 0 :(得分:2)

a prohibit sign showed up eventhough I set the allowdrop of treeview to true
您是否已将ListBox的AllowDrop设置为true

'冻结'可能是由于你的消息提示。

<强>更新
这是TreeView和ListBox的一个很好的示例代码:
Drag and Drop Using C#

答案 1 :(得分:1)

粗略地说,程序冻结的原因是消息框和拖放子系统正在争夺鼠标。您没有说明在显示消息框或解除消息时程序是否冻结,但我猜测问题是拖放子系统仍在捕获鼠标,从而阻止您单击消息框按钮。或者消息框已经破坏了捕获,拖放系统仍然试图运行一个现在永远无法完成的循环。

解决方案是将MessageBox.Show更改为Trace.WriteLine。这将为您提供所需的诊断输出,而不会干扰鼠标处理。