Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox SelectedValue question... 1

Status
Not open for further replies.

flynbye

Programmer
Nov 16, 2001
68
0
0
US
Ok get the feeling I'm trying to paddle uphill here...

What I was hoping to do (and it does not appear to be working) is to fill a listbox on the fly from an employee's table and then use the "EmpCode" listed in the code below later to properly fill a data field for a submitted meeting table. Unfortunately all I seem to be able to get back from the control is the text values from the listbox and I seem to be losing the DataValueField that I'm adding? Suggestions?

Me.ListBox1.Items.Clear()
For Each row In ds.Tables(0).Rows
lbValues = row("License") + " " + row("Fname") + " " + row("LName")
Me.ListBox1.DataValueField = row("EmpCode")
Me.ListBox1.Items.Add(lbValues)
Next

Thanks guys always appreciate the assistance!
CF

I always makes things much harder than they should be... that way later I can slap myself in the forhead after spending hours and hours on two lines of code and say to myself "DUH!"
 
Try this (from
Code:
    Dim strSQL As String = "Select Code,Description From MyTable"
    Dim Connection As New OleDbConnection("PROVIDER=....")
    Dim DA As New OleDbDataAdapter(strSQL, Connection)
    Dim DS As New DataSet

    DA.Fill(DS, "Codes")

    Dim dt As New DataTable
    dt.Columns.Add("Description", GetType(System.String))
    dt.Columns.Add("Code", GetType(System.String))
    '
    ' Populate the DataTable to bind to the Combobox.
    '
    Dim drDSRow As DataRow
    Dim drNewRow As DataRow

    For Each drDSRow In DS.Tables("Codes").Rows()
        drNewRow = dt.NewRow()
        drNewRow("Description") = drDSRow("Description")
        drNewRow("Code") = drDSRow("Code")
        dt.Rows.Add(drNewRow)
    Next

    ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList

    With ComboBox1
        .DataSource = dt
        .DisplayMember = "Description"
        .ValueMember = "Code"
        .SelectedIndex = 0
    End With


To select an item based on the "Code" or ValueMember property:

Code:
    Dim aIndex As Integer

    With ComboBox1
        For aIndex = 0 To .Items.Count - 1
            If CType(.Items(aIndex)(1), String).Trim = TextBox2.Text.Trim Then
                .SelectedIndex = aIndex
                Exit For
            End If
        Next

        If aIndex >= .Items.Count Then .SelectedIndex = -1
    End With
 
Ok not exactly on the mark but you've given me something to take a look at... soooo... STAR for you and I'll come back with the completed post ~assuming that I can make this onery bugger work :)~

CF

I always makes things much harder than they should be... that way later I can slap myself in the forhead after spending hours and hours on two lines of code and say to myself "DUH!"
 
The other possibility is using a small class to store your data in the listbox. This is C# code, but it's equally applicable to VB.NET
Code:
   private class NameLicenseDisplay
   {
      public string License;
      public string FName;
      public string LName;
      public override string ToString()
      {
         return this.License + " " + this.FName + " " +
            this.LName;
      }
   }


   ListBox1.Items.Clear();
   foreach (row in ds.Tables(0).Rows)
   {
      NameLicenseDisplay nld = new NameLicenseDisplay();
      nld.License = row("License");
      nld.FName = row("Fname");
      nld.LName = row("LName");
      ListBox1.Items.Add(nld);
   }
This works because the ListBox control calls the ToString method on the contents in order to display it. The big advantage of this (and it's a really nice one) is when the user selects something from the ListBox, the SelectedItem is a NameLicenseDisplay object, and all your values are there and easy to use. No need to take an Id value and look stuff up in the database to get the other values, or parse apart a string.

Maybe someone can translate it into correct VB.NET (I've tried in the past, and always gotten it wrong).

Chip H.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Perhaps something like this ...
Code:
   Private Class NameLicenseDisplay
      Public License As String
      Public FName As String
      Public LName As String
      Public Overrides Function ToString() As String
         Return Me.License + " " + Me.FName + " " +
            Me.LName
      End Function
   End Class
 
 
   ListBox1.Items.Clear()
   For Each row In ds.Tables(0).Rows
      Dim nld As NameLicenseDisplay =  New NameLicenseDisplay() 
      nld.License = row("License")
      nld.FName = row("Fname")
      nld.LName = row("LName")
      ListBox1.Items.Add(nld)
   Next

'----------------------------------------------------------------
' Converted from C# to VB .NET using CSharpToVBConverter(1.2).
' Developed by: Kamal Patel ([URL unfurl="true"]http://www.KamalPatel.net)[/URL] 
'----------------------------------------------------------------

This code was generated by a converter (I've left the credits in). Note that the code will fail with Option Strict On.


Hope this helps.

[vampire][bat]
 
Class approach does seem better, but I wonder if this becomes unwieldy if you have a lot of these?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top