C# Why is a treeview node clicked when canceled is set to true?
In my winform application I have a treeview. To give the idea that a node is disabled I set the node's forecolor and I when the BeforeSelect fires I set e.cancel to true.
However when a node's tag meets some criteria a node may be selected. So that's why I have a NodeMouseClick. But when a node may not be clicked I set the e.cancel to true in the BeforeSelect event.
The problem 开发者_StackOverflow中文版now is, the NodeMousClick event is always fired and all the code is ececuted, even when I set e.cancel to true in the BeforeSelect event. Why is that?
Because the node won't be / become selected (you've cancelled that out), but still being clicked upon.
When you've completed your "check whether node can be selected routine" and not-cancelling it (therefor: enabled the click); check if the node is selected
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node != null && e.Node.IsSelected)
{ //dostuff;
}
I would assume that setting Cancel
to true
in the BeforeSelect
event would rather prevent AfterSelect
from being raised. After all, the node was still clicked. The question is whether the click leads to the node being selected or not.
If possible, I would move the logic into the AfterSelect
event handler.
精彩评论