0

I have very strange problem with hiding CheckBoxes in a TreeView. Everything works as it should when TreeView is on the first TabPage in the Window but when TreeView in on the second TabPage then the first TreeNode always has checkbox.

root node without checkbox enter image description here

The code I'm using to hide some checkboxes looks like this:

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;

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;
}

[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);


private void TurnOff(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(this.Handle, TVM_SETITEM, IntPtr.Zero, lparam);
    Marshal.FreeHGlobal(lparam);
}

If someone knows solution for this problem, please share.

torpederos
  • 761
  • 12
  • 31
  • Just guessing: Would it help to first select the 2nd Page? – TaW Apr 02 '14 at 07:49
  • Try to modify the treeview properties: set CheckBoxes to false. – user3165438 Apr 02 '14 at 08:26
  • disabling all checkboxes isn't a solution because I want them in child nodes. They should only be invisible in parent nodes – torpederos Apr 02 '14 at 08:28
  • 1
    Take a look [here](http://stackoverflow.com/questions/3819108/treeview-checkboxes-only-in-child-nodes) – user3165438 Apr 02 '14 at 08:31
  • 1
    This is a rather regrettable hack that has appeared in several SO questions. The nasty memory leak in TurnOff() certainly is problematic, calling Marshal.FreeHGlobal() is not optional. The hard-coded "wezel" is probably what you are complaining about, make that "node". – Hans Passant Apr 02 '14 at 10:02
  • @Hans Passant thank you for your remark – torpederos Apr 02 '14 at 10:20
  • ALSO: if you use this method of hiding be ready to see your hidden checkboxes again when you press a space key. It will check focused checkboxes and return their visibility. – Andark Jul 04 '14 at 07:58

1 Answers1

0

Working solution can be found here: http://dotnetfollower.com/wordpress/2011/05/winforms-treeview-hide-checkbox-of-treenode/

torpederos
  • 761
  • 12
  • 31