Use instances of the TreeView control in WinForm

Source: Internet
Author: User

Create a new form that requires the following buttons on the form interface

(a TreeView a TextBox three button buttons)











The background code is as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacewindowsformsapplication1{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidForm1_Load (Objectsender, EventArgs e)            {treeView1.Nodes.Clear (); TreeNode Tree=NewTreeNode ("root node");        TREEVIEW1.NODES.ADD (tree); }        /// <summary>        ///ways to add child nodes/// </summary>         Public voidAddchildcode () {//first determine if the position in the node is selected            if(Treeview1.selectednode = =NULL) {MessageBox.Show ("Please select a node! ","Prompt Information", MessageBoxButtons.OK, messageboxicon.information); }            Else            {                if(TextBox1.Text! ="")                {                    //creates a Node object and initializesTreeNode tmp =NewTreeNode (TextBox1.Text); //to join a child node in the TreeView componentTREEVIEW1.SELECTEDNODE.NODES.ADD (TMP); Treeview1.selectednode=tmp;                Treeview1.expandall (); }                Else{MessageBox.Show ("The textbox component must fill in the node name! ","Prompt Information", MessageBoxButtons.OK, messageboxicon.information); return; }            }        }        /// <summary>        ///ways to add sibling nodes/// </summary>         Public voidaddparent () {Try            {                if(Treeview1.selectednode = =NULL) {MessageBox.Show ("Please select a node! ","Prompt Information", MessageBoxButtons.OK, messageboxicon.information); }                Else                {                    if(TextBox1.Text! =NULL)                    {                        //creates a Node object and initializesTreeNode tmp =NewTreeNode (TextBox1.Text); //to join a sibling node in the TreeView componentTREEVIEW1.SELECTEDNODE.PARENT.NODES.ADD (TMP);                    Treeview1.expandall (); }                    Else{MessageBox.Show ("The textbox component must fill in the node name! ","Prompt Information", MessageBoxButtons.OK, messageboxicon.information); return; }                }            }            Catch{TreeNode RMP=NewTreeNode ("root node");            TREEVIEW1.NODES.ADD (RMP); }        }        /// <summary>        ///Judging mouse click events/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidTreeview1_mousedown (Objectsender, MouseEventArgs e) {            //gets whether the right mouse button is clicked            if(E.button = =mousebuttons.right) {contextmenustrip1.show ( This,NewPoint (e.x, e.y)); }        }        /// <summary>        ///Expand Next Node/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidButton1_Click (Objectsender, EventArgs e)        {TreeView1.SelectedNode.Expand (); }        /// <summary>        ///Expand All Nodes/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidButton2_Click (Objectsender, EventArgs e) {            //locating the root nodeTreeview1.selectednode = treeview1.nodes[0]; //Expand all nodes in the componentTreeView1.SelectedNode.ExpandAll (); }        /// <summary>        ///Collapse All nodes/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidButton3_Click (Objectsender, EventArgs e) {            //locating the root nodeTreeview1.selectednode = treeview1.nodes[0]; //shrink all nodes in a componentTreeView1.SelectedNode.Collapse (); }        /// <summary>        ///When you click the "Join child Nodes" menu item on the shortcut menu, the user-defined Addchildcode () method is called/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidJoin child node Toolstripmenuitem_click_1 (Objectsender, EventArgs e)        {Addchildcode (); }        /// <summary>        ///Click the "Join Sibling node" menu item on the shortcut menu to invoke the user-defined Addparent () method/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidJoin the sibling node Toolstripmenuitem_click_1 (Objectsender, EventArgs e)        {addparent (); }        /// <summary>        ///Click the Delete node menu item on the shortcut menu to delete the node/// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        Private voidDelete Node Toolstripmenuitem_click (Objectsender, EventArgs e) {            if(TreeView1.SelectedNode.Nodes.Count = =0) {treeView1.SelectedNode.Remove (); }            Else{MessageBox.Show ("Please delete the child nodes in this node first! ","Prompt Information", MessageBoxButtons.OK, messageboxicon.information); }        }            }}

Use instances of the TreeView control in WinForm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.