禁用一个TreeView节点中的复选框

时间:2016-10-06 13:43:16

标签: c# .net winforms

我在TreeView Node中禁用checkBox时遇到问题。

我想在Node有孩子时禁用复选框。

这是我的TreeView:

enter image description here

代码:

        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());
                }
            }
        }

我想禁用checkBox on&#34; formularzA3&#34; - 只有在这个。关于孩子的复选框应该在这里。

有人能帮助我做到这一点吗?

2 个答案:

答案 0 :(得分:1)

这应该可以胜任。如果节点有子节点,则删除复选框

ScrollViewer

<强>用法

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

答案 1 :(得分:0)

这是一个简单的实现..:

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

您需要决定如何处理孩子......