C # Treeview Control

Source: Internet
Author: User

If the tree structure is not filled with data from the database, you do not want to use the "TreeNode" editor,

The following code can be used:

TreeNode RootNode = new TreeNode ();
TreeNode SonNode1 = new TreeNode ("Son 1 ");
TreeNode SonNode2 = new TreeNode ("Son 2 ");

RootNode. Text = "report statistics ";

RootNode. Nodes. Add ("Node 1 ");
RootNode. Nodes. Add ("Node 2 ");
RootNode. Nodes. Add ("Node 3 ");


SonNode1.Nodes. Add ("Node 11 ");
SonNode1.Nodes. Add ("node 12 ");

SonNode2.Nodes. Add ("Node 21 ");
SonNode2.Nodes. Add ("Node 22 ");

RootNode. Nodes. Add (SonNode1 );
RootNode. Nodes. Add (SonNode2 );
TreeView1.Nodes. Add (RootNode );

Shows the result:

This is just a small example. We can Add SonNode1.Nodes. Add (Key, Text) as follows:

SonNode1.Nodes. Add ("Node 11", "name ");

You can add images if they are nice.

SonNode1.Nodes. Add ("Node 11", "name", Imageindex, SelectImageIndex );

Imageindex: The image to be displayed. SelectImageIndex: The image to be displayed for this node.

 

-------------------- Another article ----------------

The TreeView component is defined by multiple classes, and the TreeView component is defined by the namespace "System. windows. the "TreeView" class in Forms "is defined, and the Node is defined by the namespace" System. windows. "TreeNode" in Forms. Therefore, when you create a TreeView object in the program, you only create a "Container" that can place nodes ". Adding a node to this container is actually adding a Node object created from the "TreeNode" class. Also, deleting a node is to delete a "TreeNode" Node object.

I. program design and running environment described in this article

(1). Microsoft Windows 2000 Server Edition

(2). Net Framework SDK official version

II. C # Some common methods and specific implementations of the Treeview component:

Although the TreeView component is difficult to operate, in the final analysis, it can be summarized as three basic operations: adding a child node, adding a sibling node, and deleting a node. Having mastered these three common operations is necessary for the flexible use of the TreeView component in programming. Next we will introduce them separately.

(1). Add a subnode:

A subnode is the next-level node of the selected node. The specific process of adding a child node is as follows: first, locate the position of the child node to be added in the TreeView component, and then create a Node object, then, Add the Node object using the Add () method in the TreeVeiw class. The following code adds a subnode to the treeView1 component:

// First, determine whether to select the position in the component.

If (treeview1.selectednode = NULL)

{

MessageBox. Show ("select a node", "message", messageboxbuttons. OK, messageboxicon. information );

}

Else

{

// Create a Node object and initialize it

Treenode TMP;

TMP = new treenode ("node name ");

// Add a subnode to the Treeview component

Treeview1.selectednode. nodes. Add (TMP );

Treeview1.selectednode = TMP;

Treeview1.expandall ();

}

(2) Add a sibling node:

A sibling node is a level node on the selected node. The method for adding a sibling node is basically the same as that for adding a child node, but there is a slight difference in the final implementation method. To add a sibling node, you must first determine the location of the sibling node to be added, define a Node object, and call the method to add a sibling node in the TreeView class, add this Node object. The biggest difference between joining a sibling node and joining a subnode is this last step. Hope you can pay attention to it. The following code adds a sibling node to the TreeView component:

// First determine whether to select the node location in the component

If (treeview1.selectednode = NULL)

{

MessageBox. Show ("select a node", "message", messageboxbuttons. OK, messageboxicon. information );

}

Else

{

// Create a Node object and initialize it

Treenode TMP;

TMP = new treenode (textbox1.text );

// Add a sibling node to the Treeview component

Treeview1.selectednode. Parent. nodes. Add (TMP );

Treeview1.expandall ();

}

(3). delete a node:

Deleting a node is to delete the selected node in the TreeView component. Deleting a node can be a subnode or a sibling node, but regardless of the nature of the node, you must ensure that the node to be deleted does not have a lower-level node. Otherwise, you must delete all the lower-level nodes of the node before deleting the node. Deleting a node is slightly simpler than the preceding two operations. The specific method is to first determine whether the node to be deleted is stored in the next-level node. If it does not exist, call Remove () in the TreeView class () to delete the node. The code for deleting a node in the TreeView component is as follows:

// Determine whether the selected node has a lower-level node

If (treeView1.SelectedNode. Nodes. Count = 0)

// Delete a node

TreeView1.SelectedNode. Remove ();

Else

MessageBox. Show ("Delete the subnode in this node first! "," Message ", MessageBoxButtons. OK, MessageBoxIcon. Information );

(4) Some other common operations of the Treeview component:

Some other common operations are much easier to implement than the preceding three operations. These common operations are to expand all nodes, expand the specified nodes, and collapse all nodes. Here is a detailed introduction:

<I>. Expand All nodes:

To expand all the nodes in the TreeView component, first locate the selected node pointer on the Root Node of the TreeView component, and then call the ExpandAll method of the selected component. The following is the specific code:

// Locate the root node

TreeView1.SelectedNode = treeView1.Nodes [0];

// Expand all nodes in the component

TreeView1.SelectedNode. ExpandAll ();

<II>. Expand the next level node of the selected node:

Because it only expands the next-level node, there is no need to use the ExpandAll () method. To Expand the next-level node, you only need to call the Expand () method. The following is the specific implementation code:

TreeView1.SelectedNode. Expand ();

<III>. collapse all nodes:

Folding all nodes and expanding all nodes is a set of interoperability, and the specific implementation ideas are also roughly the same. Folding all nodes also requires first locating the selected node pointer on the root node, then you can call the collapse () of the selected component. The specific implementation code is as follows:

// Locate the root node

TreeView1.SelectedNode = treeView1.Nodes [0];

// Collapse all nodes in the component

TreeView1.SelectedNode. Collapse ();

Now, the common methods and general methods for operating the Treeview component in C # have been introduced.

III. C # example of a complete Treeview component:

The following is an example of writing a Treeview component in C #. In this example, some of the most common operations of the Treeview component are basically overwritten by combining the common and general methods described above. For example, you can add sub-nodes, sibling nodes, delete nodes, collapse, and expand the Treeview component in a flexible program. Among them, the first three basic operations are implemented through the function in the program to play the food order, and the subsequent operations are implemented through the buttons in the program. The Code section of this program (Treeview. CS) is as follows ):

Using system;

Using System. Drawing;

Using System. Collections;

Using System. ComponentModel;

Using System. Windows. Forms;

Using System. Data;

Namespace: fully understand how to use the TreeView component

{

/// Summary of Form1.

Public class Form1: Form

{

* ****** TreeView treeView1;

* ****** Button button1;

* ****** Button button2;

* ****** Button button3;

* ****** Menuitem menuitem2;

* ****** Menuitem menuitem3;

* ****** Menuitem menuitem4;

* ****** Contextmenu contextmenu1;

* ****** Textbox textbox1;

* ****** Label label1;

/// Required designer variables.

* ****** System. componentmodel. Container components = NULL;

Public form1 ()

{

// Initialize components in the form

Initializecomponent ();

}

/// Clear all resources in use.

Protected override void dispose (bool disposing)

{

If (disposing)

{

If (components! = NULL)

{

Components. Dispose ();

}

}

Base. Dispose (disposing );

}

* ****** Void initializecomponent ()

{

// Initialization code (omitted)

}

[STAThread]

Static void Main ()

{

Application. Run (new Form1 ());

}

* ****** Void AddChildNode ()

{

// First, determine whether to select the position in the component.

If (treeView1.SelectedNode = null)

{

MessageBox. Show ("select a node", "message", MessageBoxButtons. OK, MessageBoxIcon. Information );

}

Else

{

If (textBox1.Text! = "")

{

// Create a Node object and initialize it

TreeNode tmp;

Tmp = new TreeNode (textBox1.Text );

// Add a subnode to the TreeView component

TreeView1.SelectedNode. Nodes. Add (tmp );

TreeView1.SelectedNode = tmp;

TreeView1.ExpandAll ();

}

Else

{

MessageBox. Show ("the textbox component must be filled in with the node name! "," Message ", messageboxbuttons. OK, messageboxicon. information );

Return;

}

}

}

* ****** Void addparent ()

{

// First determine whether to select the node location in the component

If (treeview1.selectednode = NULL)

{

MessageBox. Show ("select a node", "message", messageboxbuttons. OK, messageboxicon. information );

}

Else

{

If (textbox1.text! = "")

{

// Create a Node object and initialize it

Treenode TMP;

TMP = new treenode (textbox1.text );

// Add a sibling node to the Treeview component

Treeview1.selectednode. Parent. nodes. Add (TMP );

Treeview1.expandall ();

}

Else

{

MessageBox. Show ("the TextBox component must be filled in with the node name! "," Message ", MessageBoxButtons. OK, MessageBoxIcon. Information );

Return;

}

}

TreeNode tnode = new TreeNode (textBox1.Text );

}

* ****** Void treeviewinclumousedown (object sender, MouseEventArgs e)

{

If (e. Button = MouseButtons. Right)

ContextMenu1.Show (this, new Point (e. X, e. Y ));

}

* ****** Void button#click (object sender, System. EventArgs e)

{

TreeView1.SelectedNode. Expand ();

}

* ****** Void menuItem2_Click (object sender, System. EventArgs e)

{

AddChildNode ();

}

* ****** Void menuItem3_Click (object sender, System. EventArgs e)

{

AddParent ();

}

* ****** Void menuItem4_Click (object sender, System. EventArgs e)

{

// Determine whether the selected node has a lower-level node

If (treeView1.SelectedNode. Nodes. Count = 0)

// Delete a node

TreeView1.SelectedNode. Remove ();

Else

MessageBox. Show ("Delete the subnode in this node first! "," Message ", MessageBoxButtons. OK, MessageBoxIcon. Information );

}

* ****** Void button2_Click (object sender, System. EventArgs e)

{

// Locate the root node

TreeView1.SelectedNode = treeView1.Nodes [0];

// Expand all nodes in the component

TreeView1.SelectedNode. ExpandAll ();

}

* ****** Void button3_Click (object sender, System. EventArgs e)

{

// Locate the root node

TreeView1.SelectedNode = treeView1.Nodes [0];

// Collapse all nodes in the component

Treeview1.selectednode. Collapse ();

}

}

}

Related Article

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.