none
Disable TreeNode

    Question

  • Is there way to Disable TreeNode Like .Enabled = False

    I need this so no one can change the CheckBox checked state

    Wednesday, October 04, 2006 12:15 AM

Answers

  • Thanks everyone I find the way to do it see below (the node checked = true)

    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

    Wednesday, October 04, 2006 11:06 PM

All replies

  • Hi Haihtomy

    I could not disable any TreeNode but I found this methods to make it the checked value of a TreeNode fixed during runtime:

    Dim SuspendEvents As Boolean = False

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Me.TreeView1.Nodes.Add("Key1", "Hi")

    Me.TreeView1.Nodes("Key1").Nodes.Add("Key1-1", "1")

    Me.TreeView1.Nodes("Key1").Nodes("Key1-1").Tag = True

    Me.TreeView1.Nodes("Key1").Nodes("Key1-1").ForeColor = SystemColors.GrayText

    Me.TreeView1.Nodes("Key1").Nodes("Key1-1").Checked = True

    Me.TreeView1.Nodes("Key1").Nodes.Add("Key1-2", "2")

    End Sub

     

    Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck

    If Not SuspendEvents Then

    SuspendEvents = True

    If Not e.Node.Tag Is Nothing Then

    e.Node.Checked = e.Node.Tag

    End If

    SuspendEvents = False

    End If

    End Sub

     

    This way you can but the value you want to remain during runtime in Tag property in the TreeNode that you want to disable. And the  SystemColors.GrayText for the forecolor is to give it the disabled control touch

    Abdullah Al-Rasheed

    Wednesday, October 04, 2006 10:51 AM
  • I'm afraid you will have to implement this functionality yourself. The checkboxes are actually images retrieved from the StateImageList property of the TreeView (if no imagelist is provided, the default images will be used). If you play around with this, together with the BeforeCheck() event (which can be cancelled), you will be able to get the desired result.

    There's an example here (with source code) that implements about everything you need: http://www.codeproject.com/cs/miscctrl/TriStateTreeViewSubmissio.asp?df=100&forumid=36686&exp=0&select=1379620

    Hope this helps,

    Wednesday, October 04, 2006 11:09 AM
  • Thanks everyone I find the way to do it see below (the node checked = true)

    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

    Wednesday, October 04, 2006 11:06 PM
  • Setting the Checked property of a node in the AfterCheck or BeforeCheck events will cause recursive behavior (e.g. the aftercheck method will get called again). In your code, the AfterCheck event will get called 2 times when the item is unchecked: 1 time when the user unchecks the item, and a second time when your code checkes the item.

    Instead of using the AfterCheck event, you might consider using the BeforeCheck event. This is an event that can be cancelled. This way you can also prevent any unwanted screen effects (like the item appearing checked for a brief period before getting unchecked again)

    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

    Regards,

    • Proposed as answer by pasx Monday, May 18, 2009 9:18 PM
    Thursday, October 05, 2006 6:12 AM
  • Hi De Bont

    This is even a better way to do it; you should use a cancelable event if available. In my example I used SusspendEvent Variable to simulate cancel parameter

    Anyways this is for record only because our guy has soled his problem. Although, I think his example will work just fine on a single node treeview.

     

    I hope that has helpful

    Abdullah Al-Rasheed

     

    Saturday, October 07, 2006 3:49 AM
  • Thanks everyone I have what I need in this code

    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

    and I dont forget the excellent informations from all of you my friends thanks again for everything

    Monday, October 09, 2006 4:42 AM
  • Sven De Bont's answer is correct.
    Using the AfterCheck event does not work satisfactorily.

    The code should be written e.Node.Checked = !e.Node.Checked with e being the eventargs received by the function and giving access to the node that was clicked.
    This way the code handles both checking and unchecking the node.

    But of course this implementation can't work as it fires up a cascade of checking events that ends up in a stack overflow.

    My implementation is as follows - excuse my C#:

    //adding nodes...
                    n.Checked = b;
                    if (!b)  { n.ForeColor = Color.Gray; }

    //handling beforecheck

            private void trvAction_BeforeCheck(object sender, TreeViewCancelEventArgs e)
            {
                if (Color.Gray == e.Node.ForeColor)  {  e.Cancel = true; }
            }
    Thanks for setting me on the right track...
    Monday, May 18, 2009 9:17 PM