The CheckBox in the asp.net winform TreeView node disables the instance.

Source: Internet
Author: User

 

It was found that TreeNode had no relevant attributes, and the CheckBox in the TreeView was basically out of control, so I went online for help and basically talked nonsense. Some heroes couldn't even tell WinForm and WebForm.

Some people say that the font color of the node is set to gray, and then:

The code is as follows: Copy code

Private void mytreeviewinclubeforecheck (object sender, TreeViewCancelEventArgs e)
        {
If (e. Node. ForeColor = Color. Gray)
E. Cancel
        }

In this way, the requirements can be basically met, but there are two problems,

1. Check boxes that you want to disable are still in the non-disabled status. They are useless only when you click the check box and are visually confused.

2. When double-clicking, mytreeviewdomainbeforecheck becomes invalid. That is, you can select or deselect the CheckBox.

So far, the network query solution has completely failed, so I am wondering if this is really difficult? Why can't anyone give a question? Are programmers like this?

So I thought of rewriting the TreeView, and my idea was to draw the Node when TreeView, if e. Node. ForeColor = color. Gray, draw a disabled ChceckBox to overwrite the current CheckBox, http://www.111cn.net

The code is as follows:

   

The code is as follows: Copy code

Public partial class MyTreeView: TreeView
    {
Private Brush B = null; // Node font color
Private Point p; // specifies the position of the CheckBox.
Public MyTreeView ()
        {
This. DrawMode = TreeViewDrawMode. OwnerDrawText; // draw the text by yourself.
        }
Protected override void OnDrawNode (DrawTreeNodeEventArgs e)
        {
B = Brushes. Black; // The default font is Black.
If (e. Node. ForeColor = Color. Gray)
            {
P = e. Bounds. Location; // Obtain the node Location
P. X = p. X-12; // overwrite the position of the default CheckBox.
CheckBoxRenderer. DrawCheckBox (e. Graphics, p, CheckBoxState. CheckedDisabled); // draw a disabled CheckBox
B = Brushes. Gray; // The font of the current node is Gray.
            }
If (e. State & TreeNodeStates. Focused )! = 0)
B = Brushes. White; // when you click a node, the font color is White.
E. Graphics. DrawString (e. Node. Text, this. Font, B, e. Bounds. Location); // draw Text
        }
    }

 


There are many states in CheckBoxState. The CheckBoxState. MixedHot three-state dialog box is shown in the following figure.

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.