locked
Treeveiw to text box RRS feed

  • Question

  • I know I have seen programs do it before but I can not seem to find the code combination to make it work. 

     

    I have a tree with checkboxes for various options, and a textbox below.  Is there a way to make the program add or remove a specific group of words from the textbox depending on what boxs in the tree are checked.  I am trying to make this program respond after the check box is checked or unchecked and not have a submit button in order to display the text in the textbox.

     

    The other problem is, I dont want the text to be completly in the order that some of the checkboxes are.  EX: Checkbox 1,2,3 are in order but checkbox 4 is a informational checkbox that needs to go on the first line of the textbox while checkboxes 1,2,3 start on the thrid line of the text box.

     

    Thanks in advance for any questions, please ask if I did not describe in enough detail.

     

    Tuesday, December 4, 2007 9:02 AM

Answers

  • Hi, BGray,

     

    Based on my understanding, you want to add/remove the text in your TextBox through checking/unchecking some node in your TreeView, don't you?

     

    I think the Lines property of a TextBox and the AfterCheck event can help you to do this job.

    For example

    Code Block

            private void Form1_Load(object sender, EventArgs e)

            {

                textBox1.Lines = new string[4]; //Setup 4 lines for the TextBox

     

                treeView1.CheckBoxes = true;

     

                TreeNode root = new TreeNode("root"); //Add nodes to the TreeView

                TreeNode node1 = new TreeNode("node1");

                TreeNode node2 = new TreeNode("node2");

                TreeNode node3 = new TreeNode("node3");

                TreeNode node4 = new TreeNode("node4");

     

                root.Nodes.AddRange(new TreeNode[] { node1, node2, node3, node4 });

     

                treeView1.Nodes.Add(root);

     

                treeView1.AfterCheck += new TreeViewEventHandler(treeView1_AfterCheck); //Do the job after the user check/uncheck the CheckBoxes

            }

     

            void treeView1_AfterCheck(object sender, TreeViewEventArgs e) //Add specific text to specific line

            {

                string[] temp = textBox1.Lines;

                if (e.Node.Text == "node1")

                {

                    if (e.Node.Checked)

                    {

                        temp[1] = "node1";                   

                    }

                    else

                    {

                        temp[1] = "";                   

                    }

                }

                if (e.Node.Text == "node2")

                {

                    if (e.Node.Checked)

                    {

                        temp[2] = "node2";                   

                    }

                    else

                    {

                        temp[2] = "";                   

                    }

                }

                if (e.Node.Text == "node3")

                {

                    if (e.Node.Checked)

                    {

                        temp[3] = "node3";                   

                    }

                    else

                    {

                        temp[3] = "";                   

                    }

                }

                if (e.Node.Text == "node4")

                {

                    if (e.Node.Checked)

                    {

                        temp[0] = "node4";                   

                    }

                    else

                    {

                        temp[0] = "";                   

                    }

                }

                textBox1.Lines = temp;           

            }

     

     

    More info

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.treeview.aftercheck.aspx

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.textbox.aspx

     

    Hope this helps,

    Regards

     

    Thursday, December 6, 2007 8:09 AM