WinForm control TreeView only partial node display CheckBox
It should be known that the use of the ShowCheckBox property in the TreeView is used to specify which nodes display the checkbox which does not appear, but the TreeView in WinForm provides only one showcheckboxes property, either all nodes display a checkbox, or none are displayed, and the specified node does not have a ShowCheckBox attribute, the following is the function of BS corresponding to the CheckBox property in the WinForm TreeView
Method 1:
A) Set the Checkboxs property of the TreeView to False
b) Associate a ImageList component with the StateImageList property of the TreeView, add a checkbox to select the picture and uncheck the picture
c) Add the NodeMouseClick event for the TreeView, using E in the event. The Node.stateimageindex property toggles the displayed picture (depending on the index switch), you can create a generic collection, such as LIST<STRING>, to hold the Hooked node item, which facilitates switching between the tick index and the non-tick picture index
D) Cons: Clicking anywhere on the tree node will trigger the NodeMouseClick event because the Treeview1_aftercheck and Treeview1_nodemouseclick events cannot be triggered. So we can only use the NodeMouseClick event.
e) Principle: WinForm in the TreeView is actually used in the picture display, check the picture is ticked, unchecked is not checked the picture, associated with the StateImageList property (default is empty, use the own picture). So if you want to implement a specified TreeNode display checkbox, the other TreeNode does not display a checkbox and needs to use TreeNode's StateImageList property
Method 2 (Recommended):
1: Initialize:
This.treeView1.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;
This.treeView1.DrawNode + = new System.Windows.Forms.DrawTreeNodeEventHandler (This.treeview_drawnode);
: 2: Set condition control needs to show checkbox
private void Treeview_drawnode (object sender, Drawtreenodeeventargs e)
{
CheckBox before hiding a node
if (E.node.imageindex <2)//Here Set the top level 2 node does not display the selection box
Hidecheckbox (This.treeview1, E.node);
E.drawdefault = true;
}
3:
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 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);
}
[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);
The code to populate the content is slightly
Only a partial checkbox is implemented
WinForm control TreeView only partial node display CheckBox