C # preliminary use of the Treeview component

Source: Internet
Author: User

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 exists 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. The first three basic operations are implemented through the functions in the pop-up menu in the program, 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
{
Private Treeview treeview1;
Private button button1;
Private button button2;
Private button button3;
Private menuitem menuitem2;
Private menuitem menuitem3;
Private menuitem menuitem4;
Private contextmenu contextmenu1;
Private textbox textbox1;
Private Label label1;
/// Required designer variables.
Private 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 );
}
Private void initializecomponent ()
{
// Initialization code (omitted)
}
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}
Private 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;
}
}
}
Private 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 );
}
Private void treeviewinclumousedown (Object sender, mouseeventargs E)
{
If (E. Button = mousebuttons. Right)
Contextmenu1.show (this, new point (E. X, E. y ));
}
Private void button#click (Object sender, system. eventargs E)
{
Treeview1.selectednode. Expand ();
}
Private void menuitem2_click (Object sender, system. eventargs E)
{
Addchildnode ();
}
Private void menuitem3_click (Object sender, system. eventargs E)
{
Addparent ();
}
Private 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 );
}
Private 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 ();
}
Private 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 ();
}
}
}

After successfully compiling Treeview. CS, the running program interface is as follows:


Figure 01: running interface of the Treeview component written in C #

  Iv. Summary:

The Treeview component is a component that makes programmers have a headache and hard to give up. This is because the Treeview component is flexible to use, the display content is hierarchical, and the "capacity" is relatively large. At the same time, programming is more difficult than other components. After having mastered the basic methods for operating the Treeview component with C # described in this article, I think the general problems in using the Treeview component should be overcome.

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.