Select a treeview node by its path

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • André Nogueira

    Select a treeview node by its path

    Hi there.
    I know you can view a node's fullpath property, but is it posible to select
    a node using its path?
    Like, tell the treeview that the node that should be selected is the node
    with the path "Users\Administ rators\John Doe"?
    Thank you in advance!

    Andre Nogueira


  • Herfried K. Wagner [MVP]

    #2
    Re: Select a treeview node by its path

    * "André Nogueira" <anog@netcabo.p t.NOSPAM> scripsit:[color=blue]
    > I know you can view a node's fullpath property, but is it posible to select
    > a node using its path?
    > Like, tell the treeview that the node that should be selected is the node
    > with the path "Users\Administ rators\John Doe"?[/color]

    Set up a hashtable that stores (fullpath, 'TreeNode') pairs. Then you
    can lookup the treenodes by their path and set the treeview's
    'SelectedNode' property to the specific node.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

    Comment

    • André Nogueira

      #3
      Re: Select a treeview node by its path

      Thanks!
      I've never used hashtables before, but I'll look it up.

      Andre Nogueira

      "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
      news:uhYcsR8iEH A.1644@tk2msftn gp13.phx.gbl...[color=blue]
      >* "André Nogueira" <anog@netcabo.p t.NOSPAM> scripsit:[color=green]
      >> I know you can view a node's fullpath property, but is it posible to
      >> select
      >> a node using its path?
      >> Like, tell the treeview that the node that should be selected is the node
      >> with the path "Users\Administ rators\John Doe"?[/color]
      >
      > Set up a hashtable that stores (fullpath, 'TreeNode') pairs. Then you
      > can lookup the treenodes by their path and set the treeview's
      > 'SelectedNode' property to the specific node.
      >
      > --
      > M S Herfried K. Wagner
      > M V P <URL:http://dotnet.mvps.org/>
      > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>[/color]


      Comment

      • André Nogueira

        #4
        Re: Select a treeview node by its path

        Worked like a charm!
        Thank you once more for all the times you had the time and patience needed
        to help me out!
        You really deserve the MVP status and its people like you that save the
        lifes of people like me =)

        André Nogueira

        "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
        news:uhYcsR8iEH A.1644@tk2msftn gp13.phx.gbl...[color=blue]
        >* "André Nogueira" <anog@netcabo.p t.NOSPAM> scripsit:[color=green]
        >> I know you can view a node's fullpath property, but is it posible to
        >> select
        >> a node using its path?
        >> Like, tell the treeview that the node that should be selected is the node
        >> with the path "Users\Administ rators\John Doe"?[/color]
        >
        > Set up a hashtable that stores (fullpath, 'TreeNode') pairs. Then you
        > can lookup the treenodes by their path and set the treeview's
        > 'SelectedNode' property to the specific node.
        >
        > --
        > M S Herfried K. Wagner
        > M V P <URL:http://dotnet.mvps.org/>
        > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>[/color]


        Comment

        • André Nogueira

          #5
          Small Problem

          I'm having a problem.
          This is the code I am using

          <...>
          Dim a As SizeTreeNode = HashTable.Item( path)
          Me.TreeView1.Se lectedNode = a
          a.Parent.Expand All
          <...>

          Why doesn't this work? What am I doing wrong?
          The node is neither selected nor expanded.
          If I do

          MsgBox(a.Text)
          MsgBox(Me.TreeV iew1.SelectedNo de.Text)


          I see that "a" is the correct node, but the selected node is the node that
          was selected prior to that statement. So what am I missing here?
          Thanks again!

          André Nogueira

          "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
          news:uhYcsR8iEH A.1644@tk2msftn gp13.phx.gbl...[color=blue]
          >* "André Nogueira" <anog@netcabo.p t.NOSPAM> scripsit:[color=green]
          >> I know you can view a node's fullpath property, but is it posible to
          >> select
          >> a node using its path?
          >> Like, tell the treeview that the node that should be selected is the node
          >> with the path "Users\Administ rators\John Doe"?[/color]
          >
          > Set up a hashtable that stores (fullpath, 'TreeNode') pairs. Then you
          > can lookup the treenodes by their path and set the treeview's
          > 'SelectedNode' property to the specific node.
          >
          > --
          > M S Herfried K. Wagner
          > M V P <URL:http://dotnet.mvps.org/>
          > V B URL:http://dotnet.mvps.org/dotnet/faqs/[/color]


          Comment

          • Herfried K. Wagner [MVP]

            #6
            Re: Small Problem

            * "André Nogueira" <anog@netcabo.p t.NOSPAM> scripsit:[color=blue]
            > This is the code I am using
            >
            > <...>
            > Dim a As SizeTreeNode = HashTable.Item( path)
            > Me.TreeView1.Se lectedNode = a
            > a.Parent.Expand All
            > <...>
            >
            > Why doesn't this work? What am I doing wrong?
            > The node is neither selected nor expanded.[/color]

            Set the treeview's 'HideSelection' property to 'False' to check if the
            node is selected even if the treeview control doesn't have the focus.
            [color=blue]
            > If I do
            >
            > MsgBox(a.Text)
            > MsgBox(Me.TreeV iew1.SelectedNo de.Text)
            >
            >
            > I see that "a" is the correct node, but the selected node is the node that
            > was selected prior to that statement. So what am I missing here?[/color]

            Are you sure you placed this code after setting the 'SelectedNode'? To
            check if the selected node has been set, use 'If
            Me.TreeView1.Se lectedNode Is a Then...' after setting the selected node.

            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: Select a treeview node by its path

              André,
              In addition to the other comments.

              You could "walk" the Node.Nodes collection to find your node of interest.
              However depending on the amount of data & how often I wanted to select a
              specific node I might go with the other suggestion.

              Here's a custom Tree View that adds a SelectedPath property, instead of
              putting a TreeView on your Form, put a MyTreeView (I will add a TreeView in
              the designer then carefully change System.Windows. Forms.TreeView to
              MyTreeView), then in code or the properties window enter the path of the
              requested Node.

              Option Strict On
              Option Explicit On

              Public Class MyTreeView
              Inherits TreeView

              <System.Compone ntModel.Default Value("")> _
              Public Property SelectedPath() As String
              Get
              If MyBase.Selected Node Is Nothing Then
              Return String.Empty
              Else
              Return MyBase.Selected Node.FullPath
              End If
              End Get
              Set(ByVal value As String)
              If value = String.Empty Then
              MyBase.Selected Node = Nothing
              Exit Property
              End If
              Dim paths() As String = Split(value, MyBase.PathSepa rator)
              Dim nodes As TreeNodeCollect ion
              Dim node As TreeNode
              For Each path As String In paths
              If nodes Is Nothing Then
              nodes = MyBase.Nodes
              ElseIf Not node Is Nothing Then
              nodes = node.Nodes
              End If
              For Each child As TreeNode In nodes
              If child.Text = path Then
              node = child
              Exit For
              End If
              Next
              Next
              If value = node.FullPath Then
              MyBase.Selected Node = node
              Else
              Throw New ArgumentOutOfRa ngeException("S electedPath", value,
              "SelectedPa th not found")
              End If

              End Set
              End Property

              End Class


              Hope this helps
              Jay


              "André Nogueira" <anog@netcabo.p t.NOSPAM> wrote in message
              news:ekHzJN8iEH A.2500@TK2MSFTN GP15.phx.gbl...[color=blue]
              > Hi there.
              > I know you can view a node's fullpath property, but is it posible to[/color]
              select[color=blue]
              > a node using its path?
              > Like, tell the treeview that the node that should be selected is the node
              > with the path "Users\Administ rators\John Doe"?
              > Thank you in advance!
              >
              > Andre Nogueira
              >
              >[/color]


              Comment

              • André Nogueira

                #8
                Re: Small Problem

                This is the code I'm using

                Dim a As SizeTreeNode = HT.Item(Lvi.pat h)
                Me.TreeView1.Se lectedNode = a
                MsgBox(Me.TreeV iew1.SelectedNo de Is a)
                MsgBox(a.Text)
                MsgBox(Me.TreeV iew1.SelectedNo de.Text)
                a.Parent.Expand All

                The first msgbox returns "false", the other two return what I have told you.
                So... What is the problem?
                SizeTreeNode is a class that inherits from the treenode but adds a few
                properties I need for my program. All nodes on the treeview are
                SizeTreeNodes and the values added to the hashtable are also SizeTreeNodes.

                André Nogueira


                "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                news:e2EzNb9iEH A.2412@TK2MSFTN GP15.phx.gbl...[color=blue]
                >* "André Nogueira" <anog@netcabo.p t.NOSPAM> scripsit:[color=green]
                >> This is the code I am using
                >>
                >> <...>
                >> Dim a As SizeTreeNode = HashTable.Item( path)
                >> Me.TreeView1.Se lectedNode = a
                >> a.Parent.Expand All
                >> <...>
                >>
                >> Why doesn't this work? What am I doing wrong?
                >> The node is neither selected nor expanded.[/color]
                >
                > Set the treeview's 'HideSelection' property to 'False' to check if the
                > node is selected even if the treeview control doesn't have the focus.
                >[color=green]
                >> If I do
                >>
                >> MsgBox(a.Text)
                >> MsgBox(Me.TreeV iew1.SelectedNo de.Text)
                >>
                >>
                >> I see that "a" is the correct node, but the selected node is the node
                >> that
                >> was selected prior to that statement. So what am I missing here?[/color]
                >
                > Are you sure you placed this code after setting the 'SelectedNode'? To
                > check if the selected node has been set, use 'If
                > Me.TreeView1.Se lectedNode Is a Then...' after setting the selected node.
                >
                > --
                > M S Herfried K. Wagner
                > M V P <URL:http://dotnet.mvps.org/>
                > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>[/color]


                Comment

                • André Nogueira

                  #9
                  Re: Small Problem

                  If I do

                  a = Me.TreeView1.Se lectedNode.Pare nt
                  Me.TreeView1.Se lectedNode = a
                  MsgBox(Me.TreeV iew1.SelectedNo de Is a)
                  MsgBox(a.Text)
                  MsgBox(Me.TreeV iew1.SelectedNo de.Text)

                  It works.
                  I did this to see if it was a problem with the selectednode property or
                  something. This sets the selected node to parent of the currently selected
                  node and it works.
                  This isn't what I want to do, this is just an experiment.
                  So what am I doing wrong with my other code?

                  André Nogueira
                  "André Nogueira" <anog@netcabo.p t.NOSPAM> wrote in message
                  news:uV1irhDjEH A.2500@TK2MSFTN GP15.phx.gbl...[color=blue]
                  > This is the code I'm using
                  >
                  > Dim a As SizeTreeNode = HT.Item(Lvi.pat h)
                  > Me.TreeView1.Se lectedNode = a
                  > MsgBox(Me.TreeV iew1.SelectedNo de Is a)
                  > MsgBox(a.Text)
                  > MsgBox(Me.TreeV iew1.SelectedNo de.Text)
                  > a.Parent.Expand All
                  >
                  > The first msgbox returns "false", the other two return what I have told
                  > you.
                  > So... What is the problem?
                  > SizeTreeNode is a class that inherits from the treenode but adds a few
                  > properties I need for my program. All nodes on the treeview are
                  > SizeTreeNodes and the values added to the hashtable are also
                  > SizeTreeNodes.
                  >
                  > André Nogueira
                  >
                  >
                  > "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                  > news:e2EzNb9iEH A.2412@TK2MSFTN GP15.phx.gbl...[color=green]
                  >>* "André Nogueira" <anog@netcabo.p t.NOSPAM> scripsit:[color=darkred]
                  >>> This is the code I am using
                  >>>
                  >>> <...>
                  >>> Dim a As SizeTreeNode = HashTable.Item( path)
                  >>> Me.TreeView1.Se lectedNode = a
                  >>> a.Parent.Expand All
                  >>> <...>
                  >>>
                  >>> Why doesn't this work? What am I doing wrong?
                  >>> The node is neither selected nor expanded.[/color]
                  >>
                  >> Set the treeview's 'HideSelection' property to 'False' to check if the
                  >> node is selected even if the treeview control doesn't have the focus.
                  >>[color=darkred]
                  >>> If I do
                  >>>
                  >>> MsgBox(a.Text)
                  >>> MsgBox(Me.TreeV iew1.SelectedNo de.Text)
                  >>>
                  >>>
                  >>> I see that "a" is the correct node, but the selected node is the node
                  >>> that
                  >>> was selected prior to that statement. So what am I missing here?[/color]
                  >>
                  >> Are you sure you placed this code after setting the 'SelectedNode'? To
                  >> check if the selected node has been set, use 'If
                  >> Me.TreeView1.Se lectedNode Is a Then...' after setting the selected node.
                  >>
                  >> --
                  >> M S Herfried K. Wagner
                  >> M V P <URL:http://dotnet.mvps.org/>
                  >> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>[/color]
                  >
                  >[/color]


                  Comment

                  • André Nogueira

                    #10
                    Re: Small Problem

                    I think I see the problem here.
                    I think the node in the Hashtable is just a copy (a clone) of the node on
                    the treeview. As such, the program sees the node on the treeview and the
                    node on the hashtable as two different nodes.
                    This can be verified if I do

                    Me.TreeView1.Se lectedNode.Node s.Add(a)

                    It will then add a copy of the node to the selected node, leaving that node
                    with two nodes which are the same.
                    Any other suggestions?

                    André Nogueira

                    "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                    news:e2EzNb9iEH A.2412@TK2MSFTN GP15.phx.gbl...[color=blue]
                    >* "André Nogueira" <anog@netcabo.p t.NOSPAM> scripsit:[color=green]
                    >> This is the code I am using
                    >>
                    >> <...>
                    >> Dim a As SizeTreeNode = HashTable.Item( path)
                    >> Me.TreeView1.Se lectedNode = a
                    >> a.Parent.Expand All
                    >> <...>
                    >>
                    >> Why doesn't this work? What am I doing wrong?
                    >> The node is neither selected nor expanded.[/color]
                    >
                    > Set the treeview's 'HideSelection' property to 'False' to check if the
                    > node is selected even if the treeview control doesn't have the focus.
                    >[color=green]
                    >> If I do
                    >>
                    >> MsgBox(a.Text)
                    >> MsgBox(Me.TreeV iew1.SelectedNo de.Text)
                    >>
                    >>
                    >> I see that "a" is the correct node, but the selected node is the node
                    >> that
                    >> was selected prior to that statement. So what am I missing here?[/color]
                    >
                    > Are you sure you placed this code after setting the 'SelectedNode'? To
                    > check if the selected node has been set, use 'If
                    > Me.TreeView1.Se lectedNode Is a Then...' after setting the selected node.
                    >
                    > --
                    > M S Herfried K. Wagner
                    > M V P <URL:http://dotnet.mvps.org/>
                    > V B URL:http://dotnet.mvps.org/dotnet/faqs/[/color]


                    Comment

                    • André Nogueira

                      #11
                      Re: Select a treeview node by its path

                      Thank you! Your treeview works great.
                      One bug though:
                      If a node has in its path a path separator character, the property won't
                      work.
                      Example:
                      A treeview with
                      - C:\
                      ----windows
                      ----system32
                      would have the path C:\\windows\sys tem32 which would cause your treeview to
                      cast an error.
                      This could be resolved by replacing every two Path separators with just one
                      until there are no more two path separator characters together. However,
                      this would not work if you have nodes with no text.
                      But this isn't a big problem to me, so... I'm good :)
                      Thanks again

                      André Nogueira

                      "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                      news:uc2rHf9iEH A.2812@tk2msftn gp13.phx.gbl...[color=blue]
                      > André,
                      > In addition to the other comments.
                      >
                      > You could "walk" the Node.Nodes collection to find your node of interest.
                      > However depending on the amount of data & how often I wanted to select a
                      > specific node I might go with the other suggestion.
                      >
                      > Here's a custom Tree View that adds a SelectedPath property, instead of
                      > putting a TreeView on your Form, put a MyTreeView (I will add a TreeView
                      > in
                      > the designer then carefully change System.Windows. Forms.TreeView to
                      > MyTreeView), then in code or the properties window enter the path of the
                      > requested Node.
                      >
                      > Option Strict On
                      > Option Explicit On
                      >
                      > Public Class MyTreeView
                      > Inherits TreeView
                      >
                      > <System.Compone ntModel.Default Value("")> _
                      > Public Property SelectedPath() As String
                      > Get
                      > If MyBase.Selected Node Is Nothing Then
                      > Return String.Empty
                      > Else
                      > Return MyBase.Selected Node.FullPath
                      > End If
                      > End Get
                      > Set(ByVal value As String)
                      > If value = String.Empty Then
                      > MyBase.Selected Node = Nothing
                      > Exit Property
                      > End If
                      > Dim paths() As String = Split(value, MyBase.PathSepa rator)
                      > Dim nodes As TreeNodeCollect ion
                      > Dim node As TreeNode
                      > For Each path As String In paths
                      > If nodes Is Nothing Then
                      > nodes = MyBase.Nodes
                      > ElseIf Not node Is Nothing Then
                      > nodes = node.Nodes
                      > End If
                      > For Each child As TreeNode In nodes
                      > If child.Text = path Then
                      > node = child
                      > Exit For
                      > End If
                      > Next
                      > Next
                      > If value = node.FullPath Then
                      > MyBase.Selected Node = node
                      > Else
                      > Throw New ArgumentOutOfRa ngeException("S electedPath",
                      > value,
                      > "SelectedPa th not found")
                      > End If
                      >
                      > End Set
                      > End Property
                      >
                      > End Class
                      >
                      >
                      > Hope this helps
                      > Jay
                      >
                      >
                      > "André Nogueira" <anog@netcabo.p t.NOSPAM> wrote in message
                      > news:ekHzJN8iEH A.2500@TK2MSFTN GP15.phx.gbl...[color=green]
                      >> Hi there.
                      >> I know you can view a node's fullpath property, but is it posible to[/color]
                      > select[color=green]
                      >> a node using its path?
                      >> Like, tell the treeview that the node that should be selected is the node
                      >> with the path "Users\Administ rators\John Doe"?
                      >> Thank you in advance!
                      >>
                      >> Andre Nogueira
                      >>
                      >>[/color]
                      >
                      >[/color]


                      Comment

                      • Herfried K. Wagner [MVP]

                        #12
                        Re: Small Problem

                        * "André Nogueira" <anog@netcabo.p t.NOSPAM> scripsit:[color=blue]
                        > a = Me.TreeView1.Se lectedNode.Pare nt
                        > Me.TreeView1.Se lectedNode = a
                        > MsgBox(Me.TreeV iew1.SelectedNo de Is a)
                        > MsgBox(a.Text)
                        > MsgBox(Me.TreeV iew1.SelectedNo de.Text)
                        >
                        > It works.
                        > I did this to see if it was a problem with the selectednode property or
                        > something. This sets the selected node to parent of the currently selected
                        > node and it works.
                        > This isn't what I want to do, this is just an experiment.
                        > So what am I doing wrong with my other code?[/color]

                        It seems that there is a problem with 'ExpandAll', if I understand your
                        posts. Mhm, I don't have an idea...

                        --
                        M S Herfried K. Wagner
                        M V P <URL:http://dotnet.mvps.org/>
                        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                        Comment

                        • Herfried K. Wagner [MVP]

                          #13
                          Re: Small Problem

                          * "André Nogueira" <anog@netcabo.p t.NOSPAM> scripsit:[color=blue]
                          > I think I see the problem here.
                          > I think the node in the Hashtable is just a copy (a clone) of the node on
                          > the treeview. As such, the program sees the node on the treeview and the
                          > node on the hashtable as two different nodes.
                          > This can be verified if I do
                          >
                          > Me.TreeView1.Se lectedNode.Node s.Add(a)
                          >
                          > It will then add a copy of the node to the selected node, leaving that node
                          > with two nodes which are the same.
                          > Any other suggestions?[/color]

                          'TreeNode' is a class, and thus not a value type. So, a reference to
                          the same instance should be stored in the hashtable.

                          I suggest to turn 'Option Strict On', this will help you to identify
                          bugs in the code more easily. In your existing code, you will have to
                          change

                          \\\
                          Dim t As TreeNode = MyNodesHashtabl e.Item(Key)
                          ///

                          to

                          \\\
                          Dim t As TreeNode = DirectCast(MyNo desHashtable.It em(Key), TreeNode)
                          ///

                          This is not the reason why your code is not working, but it's at least a
                          starting point.

                          --
                          M S Herfried K. Wagner
                          M V P <URL:http://dotnet.mvps.org/>
                          V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                          Comment

                          • André Nogueira

                            #14
                            Re: Small Problem

                            I had already tried with CType, but to no avail.
                            Will try that now.

                            Andre Nogueira

                            "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                            news:%235lSepEj EHA.2992@TK2MSF TNGP12.phx.gbl. ..[color=blue]
                            >* "André Nogueira" <anog@netcabo.p t.NOSPAM> scripsit:[color=green]
                            >> I think I see the problem here.
                            >> I think the node in the Hashtable is just a copy (a clone) of the node on
                            >> the treeview. As such, the program sees the node on the treeview and the
                            >> node on the hashtable as two different nodes.
                            >> This can be verified if I do
                            >>
                            >> Me.TreeView1.Se lectedNode.Node s.Add(a)
                            >>
                            >> It will then add a copy of the node to the selected node, leaving that
                            >> node
                            >> with two nodes which are the same.
                            >> Any other suggestions?[/color]
                            >
                            > 'TreeNode' is a class, and thus not a value type. So, a reference to
                            > the same instance should be stored in the hashtable.
                            >
                            > I suggest to turn 'Option Strict On', this will help you to identify
                            > bugs in the code more easily. In your existing code, you will have to
                            > change
                            >
                            > \\\
                            > Dim t As TreeNode = MyNodesHashtabl e.Item(Key)
                            > ///
                            >
                            > to
                            >
                            > \\\
                            > Dim t As TreeNode = DirectCast(MyNo desHashtable.It em(Key), TreeNode)
                            > ///
                            >
                            > This is not the reason why your code is not working, but it's at least a
                            > starting point.
                            >
                            > --
                            > M S Herfried K. Wagner
                            > M V P <URL:http://dotnet.mvps.org/>
                            > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>[/color]


                            Comment

                            Working...