I'm trying to hide the checkbox infront of all my parent nodes in my treeview, but I can't get it to work. I also tried to modify the color of the text and background but that does not work either. I tried placing the call to the function in the form load, also in the contructor. Here is the functions I am trying to call
//Hide Checkbox
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; [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Auto)] private 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; } [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref TVITEM lParam); /// <summary> /// Hides the checkbox for the specified node on a TreeView control. /// </summary> private void HideCheckBox(TreeView tvw, TreeNode node) { TVITEM tvi = new TVITEM(); tvi.hItem = node.Handle; tvi.mask = TVIF_STATE; tvi.stateMask = TVIS_STATEIMAGEMASK; tvi.state = 0; SendMessage(tvw.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi); }
This is how I was colling it (in the constructor of my form)
foreach (TreeNode tn in treeView1.Nodes) { // if the text properties match, color the item if (tn.Text == "NODE0") { tn.BackColor = Color.Yellow; HideCheckBox(this.treeView1, tn); } }