2

I have problem with disabling checkBox in TreeView Node.

I want to disable checkbox when Node has childen.

Here is mine TreeView :

enter image description here

And code for it :

        for (int i = 0; i < dataTableMateriały.Rows.Count; i++)
        {
            treeNode = tree.Nodes.Add(dataTableMateriały.Rows[i][0].ToString());
            if (treeNode.Text.Contains("A3"))
            {
                for (int j = 0; j < dataTablePoddruki.Rows.Count; j++)
                {
                    treeNode.Nodes.Add(dataTablePoddruki.Rows[i][0].ToString());
                }
            }
        }

I want to disable checkBox on "formularzA3" - only in this. Checkboxes on childs should be here.

Can someone help me accomplish this?

Mati
  • 389
  • 3
  • 6
  • 16

2 Answers2

2

This should do the job. It removes the check box if the node has childs

public const int TVIF_STATE = 0x8;
    public const int TVIS_STATEIMAGEMASK = 0xF000;
    public const int TV_FIRST = 0x1100;
    public const int TVM_SETITEM = TV_FIRST + 63;

    public struct TVITEM
    {
        public int mask;
        public IntPtr hItem;
        public int state;
        public int stateMask;
        [MarshalAs(UnmanagedType.LPTStr)]
        public String lpszText;
        public int cchTextMax;
        public int iImage;
        public int iSelectedImage;
        public int cChildren;
        public IntPtr lParam;
    }

    [DllImport("user32.dll")]
    static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);


    private void RemoveCheckBoxes(TreeView tree)
    {
        List<TreeNode> nodes = new List<TreeNode>();
        foreach (TreeNode n in tree.Nodes)
        {
            if(n.Nodes.Count > 0)
            {
                 nodes.AddRange(GetNodes(n));
            }
        }

        foreach (TreeNode n in nodes)
        {
            TVITEM tvi = new TVITEM();
            tvi.hItem = n.Handle;
            tvi.mask = TVIF_STATE;
            tvi.stateMask = TVIS_STATEIMAGEMASK;
            tvi.state = 0;
            IntPtr lparam = Marshal.AllocHGlobal(Marshal.SizeOf(tvi));
            Marshal.StructureToPtr(tvi, lparam, false);
            SendMessage(this.treeView1.Handle, TVM_SETITEM, IntPtr.Zero, lparam);
        }
    }

    private List<TreeNode> GetNodes(TreeNode node)
    {
        List<TreeNode> nodes = new List<TreeNode>();
        if(node.Nodes.Count > 0)
            nodes.Add(node);
        foreach (TreeNode n in node.Nodes)
        {
            if (node.Nodes.Count > 0)
            {
                nodes.AddRange(GetNodes(n));
            }
        }
        return nodes;
    }

Usage

private void button1_Click(object sender, EventArgs e)
    {
        RemoveCheckBoxes(treeView1);
    }
Pepernoot
  • 3,409
  • 3
  • 21
  • 46
0

This is a simple implementation..:

private void tree_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
    // disable checking of nodes with children:
    if (e.Node.Nodes.Count > 0) e.Cancel = true;
    // disable checking of children:
    if (e.Node.Parent != null) e.Cancel = true;
}

You need to decide what to do with the children..

TaW
  • 53,122
  • 8
  • 69
  • 111
  • I tried this but i was still possible to check the node with child's – Pepernoot Oct 06 '16 at 14:17
  • Yes. That is what you wrote. What do you want? No node with children and also no node with a parent? Simple. I have added the extra line.. Or did you mean you could still check the node with children??? Then have have forgotten to [hook up the event](http://stackoverflow.com/questions/33275763/copy-datagridview-values-to-textbox/33276161?s=14|0.0000#33276161)- You can't just copy it into the code; it must be __hooked up__ to the event!! – TaW Oct 06 '16 at 14:52
  • I did hook it to the BeforeCheck event ! – Pepernoot Oct 06 '16 at 17:32
  • Well, it works here. Does the event code get hit in the debugger? – TaW Oct 06 '16 at 17:46