override root/parent node in tree menu
-
currently have a tree menu that allowes nodes to be added and selected, however i am trying to make it so that the parent/root node can't be selected. to achieve this i have been through if statements but if anybody could post some code it would be most helpful! thanks in advance :)
-
currently have a tree menu that allowes nodes to be added and selected, however i am trying to make it so that the parent/root node can't be selected. to achieve this i have been through if statements but if anybody could post some code it would be most helpful! thanks in advance :)
a) Add an event handler for the
TreeView.BeforeSelect
event. or b) Override theTreeView.OnBeforeSelect
method of your TreeView-inherited control. Then, check theTreeViewCancelEventArgs.Node
property and setTreeViewCancelEventArgs.Cancel
to false to prevent selecting the node. Example:private void treeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
TreeNode parent = e.Node.Parent;
if (parent == null)
{
// the root node(s) can't be selected
e.Cancel = true;
}
else if (parent.Parent == null)
{
// the node(s) in the root node(s) can't be selected
e.Cancel = true;
}
} -
a) Add an event handler for the
TreeView.BeforeSelect
event. or b) Override theTreeView.OnBeforeSelect
method of your TreeView-inherited control. Then, check theTreeViewCancelEventArgs.Node
property and setTreeViewCancelEventArgs.Cancel
to false to prevent selecting the node. Example:private void treeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
TreeNode parent = e.Node.Parent;
if (parent == null)
{
// the root node(s) can't be selected
e.Cancel = true;
}
else if (parent.Parent == null)
{
// the node(s) in the root node(s) can't be selected
e.Cancel = true;
}
}cool thanks will give it a try