开发者

Can't move treenode to listbox in c#

开发者 https://www.devze.com 2022-12-10 21:29 出处:网络
I am having a problem moving a treenode in a treeview to a listbox. The code is pretty straight forward. I just can\'t figure out why. Here is my code:

I am having a problem moving a treenode in a treeview to a listbox. The code is pretty straight forward. I just can't figure out why. Here is my code:

In window form's constructor I have:

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

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

and then the handler functions:

private void ScriptT开发者_运维问答estTreeView_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));
}

I noticed that when I was trying to drag a node from treeview, a prohibit sign showed up even though I set the allowdrop of treeview to true.

When I move the mouse to listbox, the message box show up but then the whole program freeze.

Is there a way to debug this problem? Did I do something wrong here?

Thanks.


a prohibit sign showed up eventhough I set the allowdrop of treeview to true
Have you set the AllowDrop of the ListBox to true?

The 'freezing' is probably due to your message prompt.

Update:
This is a good sample code for TreeView and ListBox:
Drag and Drop Using C#


The reason your program is freezing is, roughly speaking, that the message box and the drag-drop subsystem are fighting over the mouse. You don't say whether the program freezes when the message box is displayed or when it is dismissed, but I'd guess the problem is that the drag-drop subsystem is still capturing the mouse, preventing you from clicking the message box buttons. Or the message box has broken the capture and the drag-drop system is left trying to run a loop that can now never finish.

The solution is to change the MessageBox.Show to a Trace.WriteLine. This will give you the diagnostic output you want without interfering with the mouse handling.

0

精彩评论

暂无评论...
验证码 换一张
取 消