Results 1 to 2 of 2

Thread: Retrieve selectedvalue from listbox/xml file

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2017

    Retrieve selectedvalue from listbox/xml file

    I haven't been working on visual basic very long but I'm running into a problem in my code.

    I have a button that reads part of an xml file into a listbox.

    In addition, I want to build a function that does the following:

    Select a wine from the Listbox. then all data from that specific wine has to be read from the xml file and then this data has to be split into different text boxes.

    "Kind" should go to the Textbox tBKind, "Name" to Textbox tbName etc.

    I'm a bit stuck on this part. I thought I could use a Split method but I think I'm doing something wrong here. does anyone have any advice for me?

    The code is as follows:

    The button to read the Name and Country fields from the XML file:

    Private Sub WineListBarButton_Click(sender As Object, e As RoutedEventArgs)
            Dim xelement As XElement = XElement.Load("Assets/Wines.xml")
            Dim xWine As IEnumerable(Of XElement) = xelement.Elements()
            Dim queryData = From Wine In xWine.Descendants("Wijn")
                            Where (Wine.Attribute("Country")).Value <> ""
                            Select Country= Wine.Attribute("Country").Value,
                                   Naam = Wine.Attribute("Name").Value
                            Order By (Country)
            For Each xWine In xWine
                WineListBox.Items.Add(xWine.@Country.PadRight(37, " ") & xWine.@Name.PadRight(30, " "))
            Next xWine
        End Sub

    The Listbox to get the selected item from the listbox:

    Private Sub WineListBox_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles WineListBox.SelectionChanged
            Dim xdoc As XDocument = XDocument.Load("Assets/Wines.xml")
            Dim queryData = From Wine In xdoc.Descendants("Wine")
                            Select Name = Wine.Attribute("Name").Value,
                       Country= Wine.Attribute("Country").Value,
                       Region= Wine.Attribute("Region").Value,
                       Kind= Wine.Attribute("Kind").Value,
                       Grape= Wine.Attribute("Grape").Value,
                       AlcoholPrct = Wine.Attribute("AlcoholPrct").Value
            For Each Wine In queryData
                'This is just a try
                TBKind.Text = WineListBox.SelectedItem.Split(Wine.Kind)
    End Sub

    A few lines from the XML file:

    <Wine Name="Canapi Grillo" Country="Italie" Region="Sicilie" Kind="Wit" Grape="Grillo" AlcoholPrct="12"/>
    <Wine Name="Miopasso Fiano" Country="Italie" Region="Sicilie" Kind="Wit" Grape="Fiano" AlcoholPrct="13"/>
    <Wine Name="Elegance Sauvignon Blanc" Country="Frankrijk" Region="Pays d'Oc" Kind="Wit" Grape="Sauvignon Blanc" AlcoholPrct="11,5"/>

    Thanks in advance for reading this and for any advice

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    South Louisiana

    Re: Retrieve selectedvalue from listbox/xml file

    This is how I would approach it:
    1. Read the XML file into an XDocument
    2. Convert each Wine element into a strongly typed class
    3. Store the conversion in a collection of some kind
    4. Bind the collection to the ListBox so that the DisplayText is the name
    5. In the SelectedValueChanged, get the SelectedValue
    6. If the SelectedValue is not nothing, then set the values of the respective TextBox controls

    Here is an example:
    Dim document = XDocument.Parse(xml)
    Dim wines = From wine In document.Descendants("Wine")
                Where wine.Attribute("Name") IsNot Nothing
                Select New Wine() With {
                    .Name = If(wine.Attribute("Name") IsNot Nothing, wine.Attribute("Name").Value, String.Empty),
                    .Country = If(wine.Attribute("Country") IsNot Nothing, wine.Attribute("Country").Value, String.Empty),
                    .Region = If(wine.Attribute("Region") IsNot Nothing, wine.Attribute("Region").Value, String.Empty),
                    .Kind = If(wine.Attribute("Kind") IsNot Nothing, wine.Attribute("Kind").Value, String.Empty),
                    .Grape = If(wine.Attribute("Grape") IsNot Nothing, wine.Attribute("Grape").Value, String.Empty),
                    .AlcoholPrct = If(wine.Attribute("AlcoholPrct") IsNot Nothing, wine.Attribute("AlcoholPrct").Value, String.Empty)
    WineListBox.DisplayText = "Name"
    WineListBox.DataSource = wines
    Then in the SelectedChanged event:
    Private Sub WineListBox_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles WineListBox.SelectionChanged
        Dim selectedValue = DirectCast(WineListBox.SelectedValue, Wine)
        If (selectedValue IsNot Nothing) Then
            tBKind.Text = selectedValue.Kind
            ' etc...
        End If
    End Sub
    Live Example:
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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