Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Label based on Listbox drawn from XML file.

Posted on 2006-04-05
2
Medium Priority
?
217 Views
Last Modified: 2010-04-23
Alright, here is the scenario.  I will try and keep this question quick as I apparently can run a question on and on.

I have a list box that derives its items from an xml file.
I would like to have it work as such:

User clicks name in Listbox...and the label next to the listbox read information from another node in the xml file.

My XML file is as follows:

 <?xml version="1.0" ?>
- <AddressBook xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <Addresses>
- <Address>
  <FirstName>Troy</FirstName>
  <LastName>Halsey</LastName>
  <CompanyName>AMS Production Group</CompanyName>
  <Address1>16986 N. Dallas Parkway</Address1>
  <City>Dallas</City>
  <Region>Texas</Region>
  <PostalCode>75248</PostalCode>
  <Country>USA</Country>
  <Email>thalsey@amspg.com</Email>
  </Address>
- <Address>
  <FirstName>Brenda</FirstName>
  <LastName>Howard</LastName>
  <CompanyName>AMS Production Group</CompanyName>
  <Address1>2000 Custer</Address1>
  <City>Richardson</City>
  <Region>Texas</Region>
  <PostalCode />
  <Country />
  <Email />
  </Address>
  </Addresses>
  </AddressBook>

My current code is as follows:

   Private Sub ListBox1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim doc As New XmlDocument
        doc.Load(Application.StartupPath & "\AddressBook.xml")

        For Each node As XmlNode In doc.SelectNodes("//LastName")
            Me.ListBox1.Items.Add(node.InnerText)
        Next
    End Sub

Thank you in advance for your help.

Troy
0
Comment
Question by:troyhalsey
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 16386114
OK, to do this you will probably need to keep your XML file loaded in memory. You can do this by moving the "Dim doc As New XmlDocument" declaration outside of the method.

You can then grab the Address node relating to the LastName selected from the listbox with something like:

    Dim doc As XmlDocument

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        doc = New XmlDocument
        doc.Load(Application.StartupPath & "\AddressBook.xml")

        For Each node As XmlNode In doc.SelectNodes("//LastName")
            Me.ListBox1.Items.Add(node.InnerText)
        Next

    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

        Dim node As XmlNode = doc.SelectSingleNode("//Address[LastName='" + ListBox1.SelectedItem + "']")
        Label1.Text = node.ChildNodes(0).InnerText

    End Sub
0
 

Author Comment

by:troyhalsey
ID: 16386184
Awesome...thanks Carl! Got it working right away!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question