0

enter image description here

enter image description here

I want to restrict the user to not check the checkbox by disabling it in vb.net windows form. But, don't want to disable the child node view. ( when the user clicks on + sign in tree view, the child node should be displayed but the checkboxes should be disabled).

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    For i As Integer = 1 To 3
      Dim node As New TreeNode("Parent #" & i.ToString)
      node.Nodes.Add("Child #1")
      node.Nodes.Add("Child #2")
      TreeView1.Nodes.Add(node)
    Next
// code for disabling the checkbox but user still want to expand the child nodes even after disabling the check box. It should just restrict the user in checking the check boxes.
  End Sub
Harry
  • 1
  • 2
  • 5
  • 1
    Is it winforms? You *probably* have to show relevant code as well. – Sinatr Aug 17 '20 at 14:08
  • Added the code. Please check it now – Harry Aug 17 '20 at 14:33
  • 1
    Does this answer your question? [How to disable a WinForms TreeView node checkbox?](https://stackoverflow.com/questions/698369/how-to-disable-a-winforms-treeview-node-checkbox) – Sinatr Aug 17 '20 at 14:39
  • In theory this is simple. You just add handlers for AfterCheck, NodeMouseClick, and NodeMouseDoubleClick. The problem is that I just did it, and what I'm seeing is very strange behavior with event triggering that causes it not to work as you would think/expect. I'm happy to share the code if you want, but I cannot call it an answer because something's not working as it should. – technonaut Aug 17 '20 at 15:49
  • Please do share it. Thanks. – Harry Aug 17 '20 at 15:59
  • https://pastebin.com/EbUVjrXL In my example, I had TextBox1 control in which I added a bit of text as events were triggered. What's strange is that I don't always see the single- and double-click events when I would expect, and the checkbox somehow ends up left checked sometimes. – technonaut Aug 17 '20 at 16:18
  • Also, why not just not show the checkboxes? `TreeView1.CheckBoxes = False` – technonaut Aug 17 '20 at 16:24

1 Answers1

0

You haven't given a lot of detail for your use case afterwards but in the past when I've done this and needed some custom selection facility (it actually involved more states than yes/no/notcheckable) I have:

  • Added a TreeView

  • Turned OFF checkboxes

  • Added an ImageList to the form

  • Added 3 images: one that looks like a tick, one that looks like no tick, and one that is blank/indicates no toggling is possible

  • Set the StateImageList (not ImageList) property of the TV to the ImageList

  • Set any node that I wish to appear as checked to have a StateImageIndex of 0

  • Set any node I wish to appear as unchecked to have a StateImageIndex of 1

  • Set any node that I wish to appear as not-checkable to have StateImageIndex 2

  • Had some code like this that toggles the state only in some cases (e.g. this in NodeMouseDoubleClick):

      If e.Node.StateImageIndex = 0 Then
          e.Node.StateImageIndex = 1
      ElseIf e.Node.StateImageIndex = 1 Then
          e.Node.StateImageIndex = 0
      End If
    

Because state 2 isn't mentioned, anything that has state 2 remains thus. You can inspect the stateimageindex to know if a node is "checked" or not.

In this animation, it's the Node 2 that is state 2. A red ring appearing and fading indicates a mouse click (two for doubleclick):

enter image description here

(I just found any old random images for this demo, I don't intend to claim that these represent tick/notick/nottickable, only that they're 3 distinct random icons)

TV setup (all done in forms designer for this demo):

enter image description here

Image List setup:

enter image description here

The treeview can cycle the state itself if checkboxes=true - it toggles nodes between the image for state 0 and the image for state 1, but you can't successfully set the state to anything else - the tree ignores any setting of StateImageIndex you make, and straight uses images 0/1 depending on the Checked state of the node:

enter image description here

Caius Jard
  • 72,509
  • 5
  • 49
  • 80