Sometimes clicking on a TreeView node doesn't select it

G

Guest

Hi;

I have this case where when I click on a line in my TreeView, it selects it
for a second, and then moves the selection back to the previous selection.
Usually I have to click 3 times to get it to stick - then it's fine from then
on.

The tree is not set to accept drag/drop. HideSelection=false, otherwaise all
is standard.

This is the only event I have for the control - and it is the only control
in the form:

private void Tree_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{

TreeNode node = treeDB.GetNodeAt(e.X, e.Y);
if (node == null)
return;
RootNode root = node.Tag as RootNode;
if (root == null)
return;

DoDragDrop(new DragDropData(app, root), DragDropEffects.Copy);
}

Any ideas?
 
J

Jeffrey Tan[MSFT]

Hi Dave,

Thanks for your post.

Your code snippet is unable to run in my sample project, because you did
not provide the definition for type RootNode and DragDropData.
I suggest you provide a complete runable sample code snippet with steps or
a sample project to help us reproduce out this issue. Then we can help you
better. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

I placed a very simple project that does this at
http://www.windward.net/tree.zip

The only control in a form is a TreeView and the only event handler for that
control is MouseDown where it calls DoDragDrop().

If you click on a tree line release the mouse (without moving it) and then
move the mouse - the selection immediately moves back to the original line.

Any ideas?
 
J

Jeffrey Tan[MSFT]

Hi Dave,

Thanks for your feedback.

Yes, with your sample project, I reproduced out this problem on my side.

After doing some research, I found that it seems that DoDragDrop method
calling in MouseDown event will prevent TreeView's selected node from
changing. If we set some debug output in treeView1_MouseDown,
treeView1_MouseUp and treeView1_BeforeSelect events, we will see that:
1. Without the DoDragDrop method calling in MouseDown, all 3 events will
fire
2. With DoDragDrop method calling in MouseDown, treeView1_BeforeSelect do
not fire, and the selection will to back to its original selected treenode.

Currently, I still did not find the root cause for this issue. However, I
want to tell that for ListView and TreeView controls, we should call
DoDragDrop method in ItemDrag event to enable the drag&drop, this is
documented in:
"Performing Drag-and-Drop Operations in Windows Forms"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/
vbtskStartingDragOperations.asp

In the above document, you will see that:
"Note Certain controls have custom drag-specific events. The ListView and
TreeView controls, for example, have an ItemDrag event."

So we should use ItemDrag event to start the drag&drop operation. Also,
placing DoDragDrop method calling in ItemDrag event will not cause this
problem.

Hope this helps.
================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top