As per TreeView Remove CheckBox by some Nodes
After doing so I have my tree-view of check-box without parent node check-box. But I am facing a problem, I am not able to change the color of a particular child node.
ie. if i try to change like
treeview1.Nodes[0].Nodes[2].BackColor=Color.Gray; is still having the same old color. Can anyone help me on this. Thanks.
Edited
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); } /// <summary> /// Handles the DrawNode event of the treeView1 control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.Forms.DrawTreeNodeEventArgs"/> instance containing the event data.</param> /// <remarks></remarks> private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e) { if (e.Node.Level == 0) HideCheckBox(e.Node.TreeView, e.Node); e.DrawDefault = true; } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { treeView1.Nodes[0].Nodes[1].BackColor = Color.Red; }
treeview1.Nodes[0].Nodes[2].BackColor=Color.Gray;?Cody Grayfrom the link you posted, hide the checkbox I want, then add the code you posted in aAfterSelectevent handler, that's all. It works normally.