Reputation: 1832
I need to prevent a click event occurring on a node when its collapsed. I still want the node to collapse and hide all the children under it but I don't want the click event being fired or the node selected if possible.
Upvotes: 0
Views: 618
Reputation: 387
I tried to used the former solution but it only worked one way. I implement a dictionary where I keep the nodes expanded/collapse stated, so when I found the state is the same it is a actual node click and not a collapsing/expanding behaviour.
public class Yourclass {
var nodeStates = new Dictionary<int, bool>();
public void addNode(Yourentity entity)
{
TreeNode node= new TreeNode(entity.Name);
node.Tag = entity;
tree.Nodes.Add(entity);
nodeStates.Add(entity.Id, true /* expanded in this case but doesn't matter */);
}
private void TreeControl_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
var entity = (Yourentity )e.Node.Tag;
bool state = nodeStates[entity.Id];
// If was expanded or collapsed values will be different
if (e.Node.Nodes.Count > 0 && (e.Node.IsExpanded != state))
{
// We update the state
nodeStates[entity.Id] = e.Node.IsExpanded;
return;
}
/* Put here your actual node click code */
}
}
Upvotes: 0
Reputation: 54433
If you only need to influence you own code you can use a flag like this:
bool suppressClick = false;
private void treeView1_Click(object sender, EventArgs e)
{
if (suppressClick) return;
// else your regular code..
}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node.IsExpanded)
{ suppressClick = false; }
else { suppressClick = true; }
}
For more control you may need to get at the windows message queue..
Upvotes: 1