The After Select event handler is used to populate the text boxes used to display information from the selected node (its Name, Text, Tag, and Parent text properties). The next region of code is used to handle TreeView events there are only two events handled in this section the TreeView's After Select event and the TreeView's click event. Private void cmnuRemoveNode_Click(object sender, EventArgs e) / Remove the selected node and it children Private void cmnuAddNode_Click(object sender, EventArgs e) / forcing the user to set the name and text properties / Add a Treeview node using a dialog box When a node is deleted, the selected node and all of its children are removed from the TreeView one thing to note here is, if you are associating an object with a node through its tag you will want to write the handler to destroy that object prior to deleting the selected node. Once the values have been collected from the dialog, the new node is populated with the information and added to the selected node of the TreeView. The tag value can be any object but in this example, the tag is limited to holding an additional string value. When a new node is requested, the application opens an instance of the New Node dialog this dialog forces the user to set the name, text, and tag values for the new node. The form class also contains a context menu this context menu contains two options one to add a new node, and one to delete an existing node. The form class constructor creates a main node in the TreeView control at runtime, the user may select this node (or any child node originating form this node) to add additional nodes to the TreeView. start off by adding a base treeview node The functionality contained in the class is broken up into several regions the class begins with the default imports, namespace declaration, and class declaration: using System The main form class is a standard window form with a few controls added the form contains a split container control on the left-hand side of the control is a TreeView control, on the right-hand side of the splitter are four group boxes the first group box contains a set of labels and text boxes used to display information about a selected node, the remaining group boxes contains labels, text boxes, and buttons used to conduct various searches of the TreeView's node collection based on the node's text, name, or tag values. There is nothing custom or fancy done with any of the TreeView related components in this application it is merely a demonstration of how to work with a TreeView within the context of a Windows Forms application. The other form class (Figure 4) is used to create new nodes within the application, this form is displayed by selecting a node from the TreeView and then selecting the "Add Node" option from the context menu. One is the main form containing the TreeView and a few controls used to display node information (Figures 1, 2, and 3) and to execute searches for a specific node or group of nodes based upon a user-supplied search term. All code supplied in support of this project is contained in two form classes. The application solution contains a single Windows Application project. This is a short article addressing some of the basics of working with a TreeView control the article will address dynamically adding TreeNodes to a TreeView control, searching the nodes to find and highlight a single node or a collection of nodes matching a search term against the TreeNode's tag, text, or name properties, and manually or programmatically selecting nodes.įigure 1: Searching a TreeView Control by the Text Property.įigure 2: Searching a TreeView Control by the Name Property.įigure 3: Searching a TreeView Control by the Name Property.įigure 4: Creating a Node with Specific Properties.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |