How to customize the Treeview

Source: Internet
Author: User
The Treeview control is often used in winform development. For example, we often use it for Layer Control when developing GIS. Treeview has many attributes that can be customized to change its appearance. However, if you still cannot meet your requirements, you do not want to spend money to buy better controls, use the custom rendering function provided by Treeview.

In this way, you can easily customize the rendering to meet the customer's needs in most cases. To achieve this, it is actually very simple, with the focus being:

Drawmode = treeviewdrawmode. ownerdrawall;
Drawnode + = customdrawnode;

After the drawmode attribute is changed, you have the right to draw each menu item. Then draw each item in your customdrawnode based on your ideas:

Private void customdrawnode (Object sender, drawtreenodeeventargs E)
{
// If you follow the default implementation, do this.
If (E. node. level> 0)
{
E. drawdefault = true;
Return;
}
// The painting work is completely the set of GDI +.
Lineargradientmode mode = lineargradientmode. vertical;
Rectangle rect = E. bounds;
// Draw the gradient background
Using (lineargradientbrush brush = new lineargradientbrush (rect, _ startcolor, _ endcolor, mode ))
{
E. Graphics. fillrectangle (brush, rect );
}
Font nodefont = _ defaultfont;
// Draw the plus or minus sign and perform some hard encoding.
E. Graphics. drawimage (E. node. isexpanded? _ Minusimage: _ plusimage), E. bounds. Location. x + 5, E. bounds. Location. Y + 3 );
// Draw text
E. graphics. drawstring (E. node. text, nodefont, brushes. black, (E. bounds. location. X + 20), (E. bounds. location. Y ));
}

The complete code is here. The enlarged and reduced images are borrowed from Google doc. Since we have always customized the Treeview control, due to its limitation, the rectangle drawn cannot exceed the height of each menu item, so we want to increase the font size, or it is quite difficult to add other controls. Fortunately, this implementation has met the basic requirements of my project. If you are not so lucky, let's look at this new Treeview control, which has extremely powerful functions and far better performance than the default Treeview. Most importantly, it is free and open-source. However, it is difficult to use and extend, so it is not an excellent control.
If you only want to perform deep mining on the default Treeview, this page can teach you how to start.

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.