[2005] [RESOLVED] Prevent TreeNode from Collapsing when DoubleClicked but...
Hi,
I have got a standard Windows Forms Treeview which contains some nodes.
I am using NodeMouseDoubleClick event to get the node and then get the ID of the node and show a form containing data that belongs to the ID.
What I am stuck at is when I double click any node, it Collapses or Expands depending on it's previous state.
I want to Prevent TreeNode from Collapsing or Expanding when DoubleClicked but allow when user clicks that tiny "+" or "-" button in front of the node.
Is this possible? Is so then could someone guide me to the solution please?
Cheers :)
Re: [2005] Prevent TreeNode from Collapsing when DoubleClicked but...
The treeview exposes some events for when a node is expanded/collapsed.
Look at the BeforeCollapse and BeforeExpand events
Using the event args in those events you can decide if you want to set e.cancel to true to prevent it from happening.
The solution may include the need to set a boolean variable in your double click code, so you know if you should set cancel to true in these events.
Re: [2005] Prevent TreeNode from Collapsing when DoubleClicked but...
Thanks Kleinma, I tried those but the trouble is when I double click a node, the before collapse event fires before that and thus expanding or collapsing the node and then the double click event happening!
Re: [2005] Prevent TreeNode from Collapsing when DoubleClicked but...
It's not ideal but you could simply undo the operation on a double-click:
vb.net Code:
Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As Object, _
ByVal e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
If e.Node.IsExpanded Then
e.Node.Collapse()
Else
e.Node.Expand()
End If
End Sub
Other than that I think you'd have to get into trapping Windows messages to know when the node was expanding or collapsing due to a double-click.
Re: [2005] Prevent TreeNode from Collapsing when DoubleClicked but...
Thanks Jim,
I knew it would come down to this solution, I had it already but I just didn't wanted to go to the WM_Messages path!
I have put this code for now, in C# actually.
Code:
private void tvwChildren_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
string sEntityID = String.Empty;
if (e.Node != null)
{
sEntityID = e.Node.Name.ToString();
}
try
{
tvwChildren.SuspendLayout();
if (e.Node.IsExpanded)
{
e.Node.Collapse();
}
else
{
e.Node.Expand();
}
}
finally
{
tvwChildren.ResumeLayout(true);
}
} // tvwChildren_NodeMouseDoubleClick
Thanks for the heads up. :)