2009-11-03 53 views
1

我在將treeview中的treenode移動到列表框時出現問題。代碼非常簡單。我無法弄清楚爲什麼。這裏是我的代碼:不能將treenode移動到c中的列表框中#

在窗口形式的構造函數中我有:

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)); 
} 

我注意到,當我試圖從樹視圖拖動節點,即使我將treeview的allowdrop設置爲true,也會出現禁止標誌。

當我將鼠標移動到列表框時,消息框出現,但整個程序都凍結了。

有沒有辦法調試這個問題?我在這裏做錯了什麼?

謝謝。

回答

1

你的程序凍結的原因大體上是消息框和拖放子系統正在爲鼠標而戰。您不會說當程序顯示消息或程序被解散時程序是否凍結,但我猜想問題在於拖放子系統仍在捕獲鼠標,導致您無法單擊消息框按鈕。或者消息框破壞了捕獲,並且拖放系統試圖運行一個現在不能完成的循環。

解決方法是將MessageBox.Show更改爲Trace.WriteLine。這將爲您提供所需的診斷輸出而不會干擾鼠標處理。

2

a prohibit sign showed up eventhough I set the allowdrop of treeview to true
您是否將列表框的AllowDrop設置爲true

「凍結」可能是由於您的消息提示。

更新:
這是TreeView和ListBox中一個很好的示例代碼:
Drag and Drop Using C#

相關問題