Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 180793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:38:55+00:00 2026-05-11T14:38:55+00:00

I need to be able to disable some of the checkboxes in a TreeView

  • 0

I need to be able to disable some of the checkboxes in a TreeView control of a WinForms application, but there’s no such functionality built-in to the standard TreeView control.

I am already using the TreeView.BeforeCheck event and cancel it if the node is disabled and that works perfectly fine.

I also change the ForeColor of the disabled nodes to GrayText.

Does anyone have a simple and robust solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T14:38:56+00:00Added an answer on May 11, 2026 at 2:38 pm

    Since there’s support in C++ we can resolve it using p/invoke.

    Here’s the setup for the p/invoke part, just make it available to the calling class.

        // constants used to hide a checkbox     public const int TVIF_STATE = 0x8;     public const int TVIS_STATEIMAGEMASK = 0xF000;     public const int TV_FIRST = 0x1100;     public const int TVM_SETITEM = TV_FIRST + 63;      [DllImport('user32.dll')]     static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,     IntPtr lParam);       // struct used to set node properties     public struct TVITEM     {         public int mask;         public IntPtr hItem;         public int state;         public int stateMask;         [MarshalAs(UnmanagedType.LPTStr)]         public String lpszText;         public int cchTextMax;         public int iImage;         public int iSelectedImage;         public int cChildren;         public IntPtr lParam;      }  

    We want to determine on a node by node basis. The easiest way to do that is on the draw node event. We have to set our tree to be set as owner drawn in order for this event, so be sure to set that to something other than the default setting.

    this.tree.DrawMode = TreeViewDrawMode.OwnerDrawText; this.tree.DrawNode += new DrawTreeNodeEventHandler(tree_DrawNode); 

    In your tree_DrawNode function determine if the node being drawn is supposed to have a checkbox, and hide it when approriate. Then set the Default Draw property to true since we don’t want to worry about drawing all the other details.

    void tree_DrawNode(object sender, DrawTreeNodeEventArgs e) {     if (e.Node.Level == 1)     {         HideCheckBox(e.Node);         e.DrawDefault = true;     }     else      {         e.Graphics.DrawString(e.Node.Text, e.Node.TreeView.Font,            Brushes.Black, e.Node.Bounds.X, e.Node.Bounds.Y);     } } 

    Lastly, the actual call to the function we defined:

    private void HideCheckBox(TreeNode node) {     TVITEM tvi = new TVITEM();     tvi.hItem = node.Handle;     tvi.mask = TVIF_STATE;     tvi.stateMask = TVIS_STATEIMAGEMASK;     tvi.state = 0;     IntPtr lparam = Marshal.AllocHGlobal(Marshal.SizeOf(tvi));     Marshal.StructureToPtr(tvi, lparam, false);     SendMessage(node.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, lparam); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to disable the selection of future dates within the
I'm using a RadTreeView to display some data and I need to be able
I have a CListCtrl with checkboxes. I want to be able to disable one
I need some help in adding the replace all functionality into my Flex project.
Users of the website need to able to store images in their area ,
Need to be able to pull Magento products into an external template. Need to
I need to be able to access a list throughout my Rails project. For
I need to be able to search through multiple 2D arrays and search the
I need to be able to create an id for a hyperlink. I have
I need to be able to strip out a condition in a string, here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.