在C#中的多个已检查节点上将文本附加到Richtextbox

时间:2014-11-11 11:27:39

标签: c# winforms treeview richtextbox

我正在使用Windows Form Application C#,当我检查几个treeview节点和按钮Click事件时,每个Treenode对应的文本将附加到richtextbox。

我在Select Treenode,Ex:

做了类似的事情
 if(treeView1.SelectedNode.Name == "First Node")
    this.richTextBox1.SelectedText = Firstline;

   elseif(treeView1.SelectedNode.Name == "Second Node")
    this.richTextBox1.SelectedText = Other Text;

但是我需要从我的树视图(On Button按事件)为Multiple Checked Node做类似的活动。 请帮帮我

1 个答案:

答案 0 :(得分:1)

因为你的问题不明确。假设 你有使用treeView1Control

的winform
       //populating treeView with dummy data
   private void Form2_Load(object sender, EventArgs e)
        {
            //set the check box true
            treeView1.CheckBoxes = true;
            TreeNode treeNode = new TreeNode("Windows");
            treeView1.Nodes.Add(treeNode);
            //
            // Another node following the first node.
            //
            treeNode = new TreeNode("Linux");
            treeView1.Nodes.Add(treeNode);
            //
            // Create two child nodes and put them in an array.
            // ... Add the third node, and specify these as its children.
            //
            TreeNode node2 = new TreeNode("C#");
            TreeNode node3 = new TreeNode("VB.NET");
            TreeNode[] array = new TreeNode[] { node2, node3 };
            //
            // Final node.
            //
            treeNode = new TreeNode("Dot Net Perls", array);
            treeView1.Nodes.Add(treeNode);
        }

        private void button3_Click(object sender, EventArgs e)
        {

           string text = traverseTreeAngGetName(treeView1.Nodes);
           this.richTextBox1.SelectedText = text;

        }
        /// <summary>
        /// it will traverse all the tree Node from 1 to N Level. if Node is Checked then get
        /// the checked node name.
        /// </summary>
        /// <param name="tr"></param>
        /// <returns></returns>
        public static string traverseTreeAngGetName(TreeNodeCollection tr){

            string str = "";

            foreach (TreeNode node in tr) {

                if (node.Checked) {
                    //here you can append any text on the base of current
                    str += node.Text + " - " ;
                }
                str += traverseTreeAngGetName(node.Nodes);
            }
            return str;
        }