Skip to content

Instantly share code, notes, and snippets.

@Danielku15
Created August 4, 2011 07:57
Show Gist options
  • Save Danielku15/1124683 to your computer and use it in GitHub Desktop.
Save Danielku15/1124683 to your computer and use it in GitHub Desktop.
A utility class for hiding checkboxes on specific TreeNodse
using System;
using System.Runtime.InteropServices
namespace WinformUtils
{
/// <summary>
/// A utility class for hiding checkboxes on
/// specific TreeNodes
/// </summary>
/// <author>
/// C# Port and Wrapping (Daniel Kuschny)
/// Based on Claes Bergefalls VB.net version:
/// https://groups.google.com/group/microsoft.public.dotnet.framework.windowsforms/browse_thread/thread/cae78920cb6ff461/0a0b6b7c0571018c
/// </author>
public class TreeNodeCheckBoxUtil
{
private const int TVIF_STATE = 0x8;
private const int TVIS_STATEIMAGEMASK = 0xf000;
private const int TV_FIRST = 0x1100;
private const int TVM_SETITEM = TV_FIRST + 63;
private struct TVITEM
{
public uint mask;
public IntPtr hItem;
public uint state;
public uint stateMask;
public IntPtr pszText;
public int cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public IntPtr lParam;
}
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
private const int CHECKBOX_HIDDEN_INDEX = 0;
/// <summary>
/// Hides the CheckBox on the specified node.
/// </summary>
/// <param name="node">The node on which to hide the CheckBox</param>
public static void HideCheckBox(TreeNode node)
{
// setting the image state index to 0
// hides the checkbox
TVITEM tvi = new TVITEM();
tvi.hItem = node.Handle;
tvi.mask = TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = CHECKBOX_HIDDEN_INDEX << 12;
SendMessage(node.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, tvi);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment