שתף באמצעות


How to disable a node in treeview control

Question

Monday, October 24, 2011 8:40 AM

Hi All,

In my application i have a treeview. And i want to disable some nodes in the treeview.

For example consider the below image...

In this if i want to disable the "Supervisor Settings" node at run time, how can i do...? (i.e., either by index value or Text value)

 

PBL (Visual Studio 2010 Ultimate)

All replies (12)

Tuesday, October 25, 2011 11:24 AM ✅Answered | 1 vote

   Class MyNode
      Inherits TreeNode

      Private _Enabled As Boolean

      Public Property Enabled() As Boolean
         Get
            Return _Enabled
         End Get
         Set(ByVal value As Boolean)
            _Enabled = True
         End Set
      End Property
   End Class

 

Class MyTree
   Inherits TreeView

   Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

      Const WM_LBUTTONDOWN As Integer = &H201

      If m.Msg = WM_LBUTTONDOWN Then
         Dim coord = m.LParam.ToInt32
         Dim x = CUShort(coord And &HFFFFUS)
         Dim y = CUShort(coord >> 16)

         Dim info = HitTest(x, y)
         Dim node = TryCast(info.Node, MyNode)

         If node IsNot Nothing AndAlso Not node.Enabled Then
            Return
         End If
      End If

      MyBase.WndProc(m)

   End Sub

End Class

 

Armin


Wednesday, October 26, 2011 7:55 AM ✅Answered | 1 vote

hi

I think that Stanav solution is the answer because i asked the question before but by role and it worked for me 

 

here's the link

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/d51e06bb-be10-4c37-934c-1d30f851d776

Best Regards...Please mark as answer if my post is helpful


Monday, October 24, 2011 8:58 AM

See the link

http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/2fe68b93-fb37-4974-963a-65f44a18ad36

 

Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck
If TreeView1.Nodes.Item(0).Nodes.Item(0).Checked = False Then
TreeView1.Nodes.Item(0).Nodes.Item(0).Checked = True
End If
End Sub

 

Private Sub TreeView1_BeforeCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeCheck
            'if you don't want the user to change the item,
            'set e.Cancel to true
            e.Cancel = True
      End Sub

Please mark those posts as answer which answers your question. Faraz


Monday, October 24, 2011 9:48 AM

Faraz,

I think u didn't understand the Question properly...

For you clarity.... first of all my tree doesn't have any check boxes.

You answer works for the tree which shows check boxes.

Secondly i don't want to check the node, I need to disable the node so that user cannot select it.

I hope u understand properly now...

PBL (Visual Studio 2010 Ultimate)


Monday, October 24, 2011 2:00 PM | 1 vote

Dis/enabling doesn't seem to be supported. At least there is no notification message available:

http://msdn.microsoft.com/en-us/library/aa924962.aspx

I would...

  1. derive a class from the Treenode class
  2. add an enabled property
  3. handle the TreeView's BeforeSelect event and set e.Cancel = True if the Enabled property of the node to be selected is False.

I guess that's what Faraz also meant, he just wrote BeforeCheck instead of BeforeSelect.

 

Armin


Monday, October 24, 2011 3:09 PM

You can handle the treeview.beforeselect event and test the node name. If it is the supervisor settings node then you set e.cancel = true.

In order not to confuse the user, when you populate the treeview, you may want to change that node forecolor to some other color such as gray to indicates it's disabled.

Private Sub TreeView1_BeforeSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeSelect
        If e.Node.Name = "SupervisorSettings" Then
            e.Cancel = True
        End If
    End Sub

 


Monday, October 24, 2011 3:22 PM

Hello PLBNRAO,

I have the same problem, not being able to disable nodes in TreeView. Hopefully, in my case, the user cannot enable the node back, so I simply Remove the node.


Tuesday, October 25, 2011 3:56 AM

Does anyone have any suggestions on this.....?PBL (Visual Studio 2010 Ultimate)


Tuesday, October 25, 2011 4:02 AM

Dis/enabling doesn't seem to be supported. At least there is no notification message available:

http://msdn.microsoft.com/en-us/library/aa924962.aspx

I would...

  1. derive a class from the Treenode class
  2. add an enabled property
  3. handle the TreeView's BeforeSelect event and set e.Cancel = True if the Enabled property of the node to be selected is False.

I guess that's what Faraz also meant, he just wrote BeforeCheck instead of BeforeSelect.

 

Armin

Armin,

Can u give any sample code in deriving a class from treenode class and add an enable property...?

I tried various methods of changing the foreground color as disabled color and handel the treeview's beforeselect event and set e.cancel = true

But we see a flicker when we select the node which we handle in beforeselect event.

I want the node to completely be disable so that the user should not even select the node... As per your suggestion user can select the node but the control will go back to the previously selected not, which gives user a flicker kind of thing....

PBL (Visual Studio 2010 Ultimate)


Tuesday, October 25, 2011 7:54 AM

Hi PBLNRAO,

How about share your code with us? I think it will be easier for members to solve your problem. you can put the key part of the code. And explain your goals with your code. I think one code is more clear than ten sentences.

 


Wednesday, October 26, 2011 7:44 AM

Hi calanghei,

Public Class Form2

    Private Sub TreeView_Settings_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView_Settings.AfterSelect

        Select Case (TreeView_Settings.SelectedNode.Text)

            Case "Project Settings"

                GrpBox_SupervisorSettings.Visible = False
                GrpBox_ProjectSettings.Visible = True

            Case "Supervisor Settings"

                GrpBox_ProjectSettings.Visible = False
                GrpBox_SupervisorSettings.Visible = True

        End Select

    End Sub

End Class

In the above form i have two group boxes, GrpBox_SupervisorSettings & GrpBox_ProjectSettings

if i disable "Project Settings" node in the tree then the user should not be able to select it all, just like when we disable any control.

Hope this helps you to understand my problem clearly....

PBL (Visual Studio 2010 Ultimate)


Wednesday, October 26, 2011 7:45 AM

Arim,

i will chk and let u know the result......

PBL (Visual Studio 2010 Ultimate)