Results 1 to 12 of 12

Thread: Combobox Tricks

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Combobox Tricks

    I would like to be able to get data from two columns of a combobox. I have found a lot of instruction for displaying multiple columns, but I really would only want to display one column and be able to insert data from more than one column.

    I have come up with a couple of ways to get the data I want, outside of using a combobox, but that defeats the purpose of what I would like to do.

    Can anyone steer me to some content that might explain how this could be done?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: Combobox Tricks

    If you bind your ComboBox to a datasource, you have the displaymember which is what you see in the. ComboBox, and the valuemember, which is an associated value.
    Alternatively, you can use a class as a custom listitem, override the ToString to return the property you want displayed in the combobox, and the other properties can be accessed in the selectedindexchanged event by casting selecteditem to your custom class…

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: Combobox Tricks

    There's several ways but it depends on your data and how you are loading the combo box - and how much effort you want to put into it.

    Use a datatable, make sure it has an ID of some kind on it. Bbind the datatable to the combobox: Set the .DisplayMember to the field name you want to display. Set the .ValueMember to the ID, then set the .DataSource of the combobox to the DataTable. Then in the selectedIndexchanged event, get the .Value of the combobox... that will give you the ID of the selected row, from there you can get the rest of the data you need.
    That's probably the simplest way, and the easiest if your data is coming from a database.

    Another way involves creating a class to hold your data and creating a List(Of) to hold it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: Combobox Tricks

    Quote Originally Posted by techgnome“ View Post
    …Then in the selectedIndexchanged event, get the .Value of the combobox..
    You mean the SelectedValue?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Combobox Tricks

    I have not yet attempted to work the suggestions, but I hope to be a little more clear about what I am after. My combobox, as it is currently set, displays one column (DisplayMember), which is also set as the ValueMember.

    This one column provides one data from the record. Additionally, I want to obtain the data from an second column from the same record. I do not need, or want, the additional column to display in the combobox. Basically what I believe I need is to have two ValueMembers, if that is possible.

    Now I will review the suggestions so far.

    how much effort you want to put into it?
    I would prefer no effort at all, but am willing to put some effort into it.

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Combobox Tricks

    As explained before, you have the ability to retrieve two values. The displaymember value and the SelectedValue value.

    Take a look,
    Code:
            Me.ComboBox1.DisplayMember = "groupid"
            Me.ComboBox1.ValueMember = "crop"
            Me.ComboBox1.SelectedValue = "crop"
            Me.ComboBox1.DataSource = Me.WaterDataSet.groups
    
       Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            MessageBox.Show(Me.ComboBox1.Text)
            MessageBox.Show(Me.ComboBox1.SelectedValue.ToString)
        End Sub

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    703

    Re: Combobox Tricks

    As explained before, you have the ability to retrieve two values.
    OK, I can see how that works (although I am unable to see any previous explanation by you), but after playing with my own solutions (really crappy but working solution), my specifications have changed. There are 3 fields I need to deal with. FullName, EmployeeID, JobTitle from Table1. I would like to display FullName and jobTitle in the combobox, but I need to update two fields in table2, Manager, ManagerID.

    Now it looks to me like your method would still work, however, I would still like to be able to display two columns instead of just the one.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: Combobox Tricks

    Look into concatenating the two fields (FullName and JobTitle) in your SQL query

  9. #9
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,206

    Re: Combobox Tricks

    OK, I can see how that works (although I am unable to see any previous explanation by you),
    Of course you can't, that was my first post in this thread. lol

    But .paul and techgnome both mentioned the two different values.
    Last edited by wes4dbt; Oct 28th, 2021 at 08:18 PM.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: Combobox Tricks

    Code:
    SELECT EmployeeID, FullName, JobTitle, CONCAT(FullName, ' ', JobTitle) AS DisplayField FROM Table1
    Then you can use DisplayField as your DisplayMember

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: Combobox Tricks

    If you bind your ComboBox to a DataSource, you can set any Field as DisplayMember and not bother with a ValueMember, just cast your SelectedItem like this...

    Code:
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim row As DataRowView = DirectCast(ComboBox1.SelectedItem, DataRowView)
        MsgBox(row.Item(columnIndex))
    End Sub

  12. #12
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,686

    Re: Combobox Tricks

    My preference is what techgnome mentioned in regards to a List(Of T).


    Here I load data using the following class.

    Screenshot Attachment 182847

    Code:
    Public Class Employee
        Public Property EmployeeID As Integer
        Public Property TitleOfCourtesy As String
        Public Property FirstName As String
        Public Property LastName As String
        Public Property Fullname As String
        Public Property BirthDate As Date
        Public Property HomePhone As String
    
        Public Overrides Function ToString() As String
            Return Fullname
        End Function
    End Class
    Read the data from where ever e.g. database, text file, json file etc.

    Code:
    Public Class Form1
        Private ReadOnly EmployeeBindingSource As BindingSource = New BindingSource()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            EmployeeBindingSource.DataSource = Operations.ReadEmployee()
            EmployeeComboBox.DataSource = EmployeeBindingSource
        End Sub
    
        Private Sub CurrentButton_Click(sender As Object, ea As EventArgs) Handles CurrentButton.Click
            Dim e = CType(EmployeeBindingSource.Current, Employee)
            MessageBox.Show($"{e.TitleOfCourtesy} {e.LastName}{vbCr}{e.BirthDate.ToShortDateString()}{vbCr}{e.HomePhone}")
        End Sub
    End Class

Tags for this Thread

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