5
I am using a TreeView
to control permissions on my system, each operator has a profile that determines their minimum access, but it is possible to give more permissions to a particular operator, without having to create another profile (for example: stock profile and inventory supervisor profile)but never remove a permission given by the profile.
For that I, populated the TreeView
using an object that extended the class TreeNode
(list 1) to which I added the property Enabled
indicating whether the property Checked
may be amended. No event BeforeCheck
of TreeView
(list 2) check if the node is Enabled = false
and cancel the event. However when double-clicking on the node it has its property Checked
altered, which hurts my restriction. I tried to cancel the event DoubleClick
of TreeView
, but signing the method does not allow me to cancel the action.
Summarizing: How to cancel the double click action or prevent the property Checked
be altered by it?
List 1
public class AccessNode : TreeNode
{
public AccessNode(string nome, string texto)
{
Name = nome;
Text = texto;
Checked = false;
}
public bool Enabled { get; set; } = true;
public void Propagate ()
{
/*propaga a alteração no "Cheked" para cima ou para baixo na arvore dependendo do nó atual*/
if (Checked)
CheckedParent();
else
UnCheckedNodes();
}
private void CheckedParent()
{
/*marca todos os nós do qual descende o nó atual, ou seja, pai, avô, bisavô, etc.*/
if (Parent != null)
{
Parent.Checked = true;
(Parent as AccessNode).CheckedParent();
}
}
private void UnCheckedNodes()
{
/*desmarca todos os nós decentendes do náo atual, ou seja, os filhos, os filhos dos filhos, etc.*/
foreach (TreeNode tn in Nodes)
tn.Checked = false;
}
}
List 2
private void treeView_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
AccessNode an = e.Node as AccessNode;
e.Cancel = !an.Enabled;
}
In the examples quotes the
TreeView
but I believe it serves inTreeNode
, for theTreeNode
represents the nodes of theTreeView
. And the right thing would be to disableTreeView
.– MeuChapeu
To
TreeNode
does not have this EventTreeView
worked perfectly, just follow the link and use the code mentioned by you.– Benjamim Mendes Junior
Show, I’m glad I’m working.
– MeuChapeu