How to disable a node in the TreeView in C # WinForm

Source: Internet
Author: User

Reprint Office: http://shancheng2007.blog.163.com/blog/static/496838952010111452015761/

No direct way to disable a node is found, so the node forecolor to be disabled is set to gray
Then, in the BeforeSelect of the TreeView, the Beforecheck event is judged by the node color, and if it is gray, the user is not selected for that node

The reference code is as follows

Using System.Drawing;

Disable First node
Treeview1.nodes[0]. ForeColor = Color.gray;

It can be done after the binding data is complete, or it can be specified during the binding process, looking at the individual and the requirements.

private void Treeview1_beforeselect (object sender, TreeViewCancelEventArgs e)
{
if (E.node! = null)
{
if (E.node.forecolor = = Color.gray)
{
E.cancel = true; Do not let select Disable node
}
}
}
private void Treeview1_beforecheck (object sender, TreeViewCancelEventArgs e)
{
if (E.node! = null)
{
if (E.node.forecolor = = Color.gray)
{
E.cancel = true; Do not let select Disable node
}
}
}


When processing a selection node

foreach (TreeNode c_tn in Tr_subdepdev.nodes)
{
if (c_tn. Checked && C_tn. ForeColor! = Color.gray)//Device node is selected
{
Here to deal with

}
}

So that we can achieve the basic purpose ~

How to disable a node in the TreeView in C # WinForm

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.