... edit #1 ...
With a quick search I found this Win API code for disabling CheckBoxes in a TreeView: [
^].
This would require you to sub-class the Microsoft TreeView, something I tried in the past and was not happy with the results of for various reasons.
... end edit #1 ...
Assuming this is a Windows Forms TreeView: there is
some good news; you can prevent CheckBox activation from "casual clicking" by doing the following:
List<TreeNode> DisabledCheckNodes = new List<TreeNode>();
private void SetDisabledCheckNodes(params TreeNode[] theNodes)
{
DisabledCheckNodes.AddRange(theNodes);
}
private void EnableCheckNodes(params TreeNode[] theNodes)
{
foreach (var theNode in theNodes)
{
if (DisabledCheckNodes.Contains(theNode)) DisabledCheckNodes.Remove(theNode);
}
}
private bool dontRecurse = false;
private bool StopCheckBoxUse(TreeNode theNode)
{
if (DisabledCheckNodes.Contains(theNode))
{
dontRecurse = true;
theNode.Checked = false;
treeView1.Update();
dontRecurse = false;
return true;
}
else
{
return false;
}
}
private void treeView1_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
if (dontRecurse) return;
e.Cancel = StopCheckBoxUse(e.Node);
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (dontRecurse) return;
StopCheckBoxUse(e.Node);
}
To set which Nodes have disabled CheckBoxes:
SetDisabledCheckNodes(treeView1.Nodes[2],treeView1.Nodes[4],treeView1.Nodes[6]);
To re-enable CheckBoxes on a TreeNode:
EnableCheckNodes(treeView1.Nodes[6]);
And now, the bad news; the user, if determined, can get the CheckBox Checked, or unchecked by quickly repeated clicking on the CheckBox.
I experimented (long ago) with defining every TreeView Event that could possibly affect the CheckBox state, all MouseClicks, MouseDowns, After-Before Select, etc., and set the CheckBox to unchecked in them, without success. I have concluded that this phenomenon is yet another quirk of the Win Form TreeView (another reason, among many, I switched to using the commercial Lidor Systems IntegralUI TreeView).
However, this code was done several years ago, and I have not researched, recently, to see if there is any other "fix" available for making disabling the CheckBoxes "stick" under all conditions for the WinForm TreeView.