选定的子节点文本检索在树视图列表中无法正常工作

时间:2013-10-19 06:08:33

标签: c# winforms checkbox treeview

在我的c#windows应用程序中,我有一个树形视图列表框,其复选框设置为true。在运行时,我的程序在xml文件中检查此树视图的节点。在我的应用程序中,我使用下面显示的代码隐藏了父节点复选框:

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;

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

    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;

    }

    void tree_DrawNode(object sender, DrawTreeNodeEventArgs e)
    {
        if (e.Node.Level == 1)
            HideCheckBox(e.Node.Parent);
        e.DrawDefault = true;
    }

    private void HideCheckBox(TreeNode node)
    {
        TVITEM tvi = new TVITEM();
        tvi.hItem = node.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.taskslctor_treeview.Handle, TVM_SETITEM, IntPtr.Zero, lparam);
    }

我现在想要的是我应该能够检查所选的任何子节点(在运行时),如果有任何选择我应该能够将所选子节点的文本添加到数组列表中(因为多个复选框可以是如果没有选中,则消息框显示为未选中。

在隐藏父复选框之前,当我尝试下面的代码时,它工作正常。但是现在(在隐藏父复选框之后)这不起作用..

public void checkednodes()
    {
        foreach (TreeNode node in taskslctor_treeview.Nodes)
        {               
            if (node.Checked)
            {
                check.Add(node.Text);
                MessageBox.Show(check.ToString());
            }
        }
        if (check.Count == 0)
        {
            MessageBox.Show("Atleast one task needs to be selected!!", "Error");
        }
    }

任何直接的帮助都会非常感激...因为我的工作现在暂停了......

1 个答案:

答案 0 :(得分:1)

根据您的评论,您似乎只需要查看child nodes,而不是level-1 node,因此这应该对您有用:

//Use this to get all the Checked child nodes under a Parent node
private List<TreeNode> GetCheckedNodes(TreeNode node){
  List<TreeNode> nodes = new List<TreeNode>();
  if(node.Checked) nodes.Add(node);
  foreach(TreeNode n in node.Nodes)
    nodes.AddRange(GetCheckedNodes(n));
  return nodes;
}


 check.Clear();
 foreach (TreeNode node in taskslctor_treeview.Nodes) {               
    foreach(TreeNode childNode in GetCheckedNodes(node)){
        check.Add(childNode.Text);            
    }
 }
 MessageBox.Show(check.ToString());
 if (check.Count == 0){
    MessageBox.Show("Atleast one task needs to be selected!!", "Error");
 }

更新

要同时获取CheckedUnchecked个节点,您应该声明分别存储节点的2 List<TreeNode>,然后您只需要一个recursive循环来填充2个列表,像这样:

List<TreeNode> checkedNodes = new List<TreeNode>();
List<TreeNode> uncheckedNodes = new List<TreeNode>();
private void FillNodeLists(TreeNode node){
  if(node.Checked) checkedNodes.Add(node);
  else uncheckedNodes.Add(node);
  foreach(TreeNode n in node.Nodes)
     FillNodeLists(n);
}
//then just use the 2 Lists (such as loop through them)