Cancel Doubleclick on Treeview

Asked

Viewed 144 times

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 TreeViewusing 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;
    }

1 answer

3


Try adding this code snippet to your class:

protected override void WndProc(ref Message m)
{
    if (m.Msg == 0x203) // identified double click
        m.Result = IntPtr.Zero;
    else 
        base.WndProc(ref m);
}

This code will disable double-click.

See more about here.

  • In the examples quotes the TreeView but I believe it serves in TreeNode, for the TreeNode represents the nodes of the TreeView. And the right thing would be to disable TreeView.

  • 1

    To TreeNode does not have this Event TreeView worked perfectly, just follow the link and use the code mentioned by you.

  • Show, I’m glad I’m working.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.