C # tree structure of language representation-advanced TreeView Usage Guide

Source: Internet
Author: User

 

Recently, due to the project relationship, Microsoft has made some research on the TreeView control provided by Microsoft in various programming languages. I checked it on the internet at first, except for some scattered knodge DGE in Microsoft's MSDN, but I didn't find any topic articles in this area. So I had the idea of writing this article, hoping to give a reference to it, to reduce the number of detours.

 

For the sake of personal interest, I mainly studied the TreeView controls in Microsoft Visual C ++ and C #. In these two languages, this document does not describe the basic usage of the TreeView control. relatively advanced usage mainly involves setting static icons and selected icons for TreeItem, and the drag and drop operations, the TreeView control in C # is a relatively complete and elegant design among all the TreeView controls provided by Microsoft. The following uses the TreeView control in C # as an example to describe the Corresponding topics.

 

Let's take a look at the definition of the C # TreeView control on MSDN:

 

Displays a hierarchical collection of labeled items, each represented by a TreeNode.

 

Translation: represents a set of labeled items with inherited structure relationships, each of which is represented by TreeNode. That is to say, the TreeNode represents the node of the tree, which is mainly responsible for its work.

 

So how to set images for TreeNode?

 

The following text is from MSDN:

 

The Image can be displayed next to the tree node by assigning an ImageList to the ImageList attribute and allocating the Image by referencing the index value of the Image in ImageList.

 

Use the following attributes to allocate images:

 

Set the ImageIndex attribute to the index value of the Image displayed when the tree node is not selected.

 

Set the SelectedImageIndex attribute to the index value of the Image to be displayed when the tree node is selected.

 

The image referenced by the ImageIndex and SelectedImageIndex attribute values is the default image displayed by all Tree Nodes allocated to the Nodes set. Each tree node can replace the default image by setting the TreeNode. ImageIndex and TreeNode. SelectedImageIndex attributes.

 

After reading this text, do you know how to operate it? I think there may be many people who do not fully understand it. Let's describe it in the programmer's language:

 

First, define an ImageList variable and assign it to the ImageList of the TreeView,

 

1 private System. Windows. Forms. ImageList imageList007;

2 this. imageList007 = new System. Windows. Forms. ImageList (this. components );

3 this. treeView1.ImageList = this. imageList007;

 

 

Then you need to set the icon,

 

 

This. imageList007.ImageStream =

(System. Windows. Forms. ImageListStreamer) (resources. GetObject ("imageList007.ImageStream ")));

This. imageList007.TransparentColor = System. Drawing. Color. Transparent;

This. imageList007.Images. SetKeyName (0, "Icon01.ico ");

This. imageList007.Images. SetKeyName (1, "Icon02.ico ");

This. imageList007.Images. SetKeyName (2, "Icon03.ico ");

This. imageList007.Images. SetKeyName (3, "Icon04.ico ");

This. imageList007.Images. SetKeyName (4, "Icon05.ico ");

 

 

Finally, you can set the ImageIndex and SelectedImageIndex attributes of the TreeView, select the corresponding image, or set an independent image for each dynamically generated node, now you have successfully set the TreeNode image.

 

Then how can we implement the Drag and Drop operations?

 

First, we need to know what the source and target of Drag and Drop are, that is, from where to Drag, and what the responsibilities of each part are, and only the corresponding processing is done on the source and target, the drag process can be complete and effective.

 

Source component processing:

 

ItemDrag event: When the Mouse starts to drag the source tree node, this event is triggered, that is, the event sender should be the source component, in this event processing function, you need to call the DoDragDrop method to initialize and start a drag process.

 

Target component processing:

 

DragEnter event: After the initialization operation is complete, you need to process the DragEnter event at the drag and drop target. This event occurs when the source node is dragged to a certain point within the scope of the target component, in this event, we can verify the validity of the drag and drop operation and set different mouse shapes to indicate different states. The DragDropEffects structure is used to set the Mouse shape.

 

 

 

Target component processing:

 

DragDrop event: this event is used to process drag events in the target component. It occurs when the source node has been dragged to the target component, in this case, we can parse the dragged nodes and perform other operations.

 

Note: To complete the drag and drop operation, you must set both the source and target components to true.

 

 

 

The following code is used to drag a TreeNode to a Form:

 

Private void UsingTreeViewDemo_Load (object sender, EventArgs e)

{

This. treeView1.ItemDrag + = new System. Windows. Forms. ItemDragEventHandler (this. treeView_ItemDrag );

This. DragEnter + = new System. Windows. Forms. DragEventHandler (this. Form_DragEnter );

This. DragDrop + = new System. Windows. Forms. DragEventHandler (this. Form_DragDrop );

}

 

Private void treeView_ItemDrag (object sender, System. Windows. Forms. ItemDragEventArgs e)

{

DoDragDrop (e. Item, DragDropEffects. Copy );

}

 

Private void Form_DragEnter (object sender, System. Windows. Forms. DragEventArgs e)

{

E. Effect = DragDropEffects. Copy;

}

 

Private void Form_DragDrop (object sender, System. Windows. Forms. DragEventArgs e)

{

TreeNode NewNode;

If (e. Data. GetDataPresent ("System. Windows. Forms. TreeNode", false ))

{

NewNode = (TreeNode) e. Data. GetData ("System. Windows. Forms. TreeNode ");

MessageBox. Show (NewNode. ToString ());

}

}

 

 

 

At this point, the mission of this article has been completed. I believe you are familiar with these two relatively advanced TreeView usage. You are welcome to discuss this topic and correct your criticism of this article.

 

Prepared by SolidMango

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.