0

在我的 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");
        }
    }

任何立即的帮助将不胜感激......因为我的工作现在被搁置了......

4

1 回答 1

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节点,您应该声明 2List<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)
于 2013-10-19T06:41:49.187 回答