Results 1 to 5 of 5

Thread: Treeview control

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    Treeview control

    Hey Guys,

    I have a treeview control on my form, i am trying to load an xml into the treeview to show cities/states (example at the bottom of the page)

    The xml that treeview is loading from looks like:

    vb.net Code:
    1. <?xml version="1.0" encoding="utf-8" ?>
    2. <Locations>
    3.   <Group name="US States">
    4.     <Group name="Alabama">
    5.       <Location name="Tuscaloosa" url="tsc/{0}" />
    6.       <Location name="Montgomery" url="mgm/{0}" />
    7.       <Location name="Mobile" url="mob/{0}" />
    8.       <Location name="Huntsville" url="hsv/{0}" />
    9.       <Location name="Gadsden-Anniston" url="anb/{0}" />
    10.       <Location name="{Florence|Muscle Shoals}" url="msl/{0}" />
    11.       <Location name="Dothan" url="dhn/{0}" />
    12.       <Location name="Birmingham" url="bhm/{0}" />
    13.       <Location name="Auburn" url="aub/{0}" />
    14.     </Group>
    15.     <Location name="Alaska" url="anc/{0}" />
    16.     <Group name="Arizona">
    17.       <Location name="Yuma" url="yum/{0}" />
    18.       <Location name="Tucson" url="tus/{0}" />
    19.       <Location name="Sierra Vista " url="fhu/{0}" />
    20.       <Location name="Prescott" url="prc/{0}" />
    21.       <Group name="Phoenix">
    22.         <Location name="{Central|South Phoenix|S. Phoenix}" url="phx/{0}/cph"/>
    23.         <Location name="East Valley" url="phx/{0}/evl"/>
    24.         <Location name="Phoenix North" url="phx/{0}/nph"/>
    25.         <Location name="West Valley" url="phx/{0}/wvl"/>
    26.       </Group>
    27.       <Location name="Mohave County" url="mhv/{0}" />
    28.       <Location name="{Flagstaff|Sedona}" url="flg/{0}" />
    29.     </Group>
    30. </locations>

    I'v never really used the treeview control before, i can load xml documents ok, i'm unsure how to then load the data into the treeview

    any help would be appreciated

    thanks guys

    Graham
    Attached Images Attached Images  

  2. #2
    Hyperactive Member DerekM's Avatar
    Join Date
    Jun 2009
    Location
    Colorado
    Posts
    296

    Re: Treeview control

    Why don't you just manually add the nodes?

  3. #3
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Treeview control

    Quote Originally Posted by DerekM
    Why don't you just manually add the nodes?
    It may be that he wants to update the list without re-coding the program.

    Graham23s, I did a quick Google search and found this.

    It looks pretty solid.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    417

    Re: Treeview control

    Hey Guys,

    That was a great example WD, i downloaded and implemented it:

    vb.net Code:
    1. Imports System.Xml
    2.  
    3. Public Class Form1
    4.     Inherits System.Windows.Forms.Form
    5.  
    6. #Region " Windows Form Designer generated code "
    7.  
    8.     Public Sub New()
    9.         MyBase.New()
    10.  
    11.         'This call is required by the Windows Form Designer.
    12.         InitializeComponent()
    13.  
    14.         'Add any initialization after the InitializeComponent() call
    15.  
    16.     End Sub
    17.  
    18.     'Form overrides dispose to clean up the component list.
    19.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    20.         If disposing Then
    21.             If Not (components Is Nothing) Then
    22.                 components.Dispose()
    23.             End If
    24.         End If
    25.         MyBase.Dispose(disposing)
    26.     End Sub
    27.  
    28.     'Required by the Windows Form Designer
    29.     Private components As System.ComponentModel.IContainer
    30.  
    31.     'NOTE: The following procedure is required by the Windows Form Designer
    32.     'It can be modified using the Windows Form Designer.  
    33.     'Do not modify it using the code editor.
    34.     Friend WithEvents trvItems As System.Windows.Forms.TreeView
    35.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    36.         Me.trvItems = New System.Windows.Forms.TreeView
    37.         Me.SuspendLayout()
    38.         '
    39.         'trvItems
    40.         '
    41.         Me.trvItems.CheckBoxes = True
    42.         Me.trvItems.Dock = System.Windows.Forms.DockStyle.Fill
    43.         Me.trvItems.Location = New System.Drawing.Point(0, 0)
    44.         Me.trvItems.Name = "trvItems"
    45.         Me.trvItems.Size = New System.Drawing.Size(292, 309)
    46.         Me.trvItems.TabIndex = 0
    47.         '
    48.         'Form1
    49.         '
    50.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    51.         Me.ClientSize = New System.Drawing.Size(292, 309)
    52.         Me.Controls.Add(Me.trvItems)
    53.         Me.Name = "Form1"
    54.         Me.Text = "Form1"
    55.         Me.ResumeLayout(False)
    56.  
    57.     End Sub
    58.  
    59. #End Region
    60.  
    61.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    62.         Dim file_name As String = Application.StartupPath
    63.         file_name = file_name.Substring(0, file_name.LastIndexOf("\")) & "\test.xml"
    64.         LoadTreeViewFromXmlFile(file_name, trvItems)
    65.     End Sub
    66.  
    67.     ' Load a TreeView control from an XML file.
    68.     Private Sub LoadTreeViewFromXmlFile(ByVal file_name As String, ByVal trv As TreeView)
    69.         ' Load the XML document.
    70.         Dim xml_doc As New XmlDocument
    71.         xml_doc.Load(file_name)
    72.  
    73.         ' Add the root node's children to the TreeView.
    74.         trv.Nodes.Clear()
    75.         AddTreeViewChildNodes(trv.Nodes, xml_doc.DocumentElement)
    76.     End Sub
    77.  
    78.     ' Add the children of this XML node
    79.     ' to this child nodes collection.
    80.     Private Sub AddTreeViewChildNodes(ByVal parent_nodes As TreeNodeCollection, ByVal xml_node As XmlNode)
    81.         For Each child_node As XmlNode In xml_node.ChildNodes
    82.             ' Make the new TreeView node.
    83.             Dim new_node As TreeNode = parent_nodes.Add(child_node.Name)
    84.  
    85.             ' Recursively make this node's descendants.
    86.             AddTreeViewChildNodes(new_node.Nodes, child_node)
    87.  
    88.             ' If this is a leaf node, make sure it's visible.
    89.             If new_node.Nodes.Count = 0 Then new_node.EnsureVisible()
    90.         Next child_node
    91.     End Sub
    92.  
    93.     Private Sub trvItems_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvItems.AfterSelect
    94.  
    95.     End Sub
    96. End Class

    simplest xml:

    PHP Code:
    <?xml version="1.0"?>
    <Tree>
        <US>
            <city>Alaska</city>
        </US>
        <UK>
            <city>Aberdeen</city>
        </UK>
    </Tree>
    Instead of displaying the city, it displays the word "city" in the treeview like:

    Is there a way i could display the innertext of the node?

    thanks guys

    Graham
    Attached Images Attached Images  
    Last edited by graham23s; Sep 13th, 2009 at 05:01 AM.

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Treeview control

    vb Code:
    1. ' Load a TreeView control from an XML file.
    2. Private Sub LoadTreeViewFromXmlFile(ByVal file_name As String, ByVal trv As TreeView)
    3.     Dim xml_doc As New XmlDocument
    4.     xml_doc.Load(file_name)
    5.     trv.Nodes.Clear()
    6.     AddTreeViewChildNodes(trv.Nodes, xml_doc.DocumentElement)
    7. End Sub
    8.  
    9. ' Add the children of this XML node to this child nodes collection.
    10. Private Sub AddTreeViewChildNodes(ByVal parent_nodes As TreeNodeCollection, ByVal xml_node As XmlNode)
    11.     For Each child_node As XmlNode In xml_node.ChildNodes
    12.         Dim new_node As TreeNode = parent_nodes.Add(GetNodeText(child_node))
    13.         AddTreeViewChildNodes(new_node.Nodes, child_node)
    14.         If new_node.Nodes.Count = 0 Then new_node.EnsureVisible()
    15.     Next
    16. End Sub
    17.  
    18. Private Function GetNodeText(ByVal node As XmlNode) As String
    19.     ''Put here whatever you want displayed as treeview text
    20.     If node.Attributes IsNot Nothing Then
    21.         For Each attr As XmlAttribute In node.Attributes
    22.             If attr.Name = "name" Then Return attr.Value
    23.         Next
    24.     End If
    25.     If node.InnerText <> "" Then Return node.InnerText
    26.     Return node.Name
    27. End Function
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width