Reputation: 896
I'm trying to create a custom acting TreeView. When you click a node it should toggle as selected/unselected. Currently I can select a node once by clicking it, deselect the node by clicking it again, but I am unable to select the node again via clicking unless I select another node first. Any help would be greatly appreciated.
TreeNode lastNode;
private void treeViewMS1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (lastNode == e.Node)
{
treeViewMS1.SelectedNode = null;
lastNode = null;
}
else
{
if (lastNode == null)
{
treeViewMS1.SelectedNode = e.Node;
}
lastNode = e.Node;
}
}
Upvotes: 1
Views: 1216
Reputation: 81610
Try using the BeginInvoke procedure to delay the action until after the mouse click event is done processing. It's probably interfering:
TreeNode lastNode;
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
this.BeginInvoke(new Action(() => {
if (lastNode == e.Node) {
treeView1.SelectedNode = null;
lastNode = null;
} else {
if (lastNode == null) {
treeView1.SelectedNode = e.Node;
}
lastNode = e.Node;
}
}));
}
If the Action method isn't available, you can use the MethodInvoker style:
this.BeginInvoke((MethodInvoker)delegate {
Upvotes: 1