none
Populating a combo box list RRS feed

  • Question

  • I have a form that contains two combo boxes. I populate the list for the first combo box based on a value entered by the user on the previous form. The user then selects / enters a value in the first combo box and tabs out of the control. The list for the second combo box is populated based on the value from the first combo box.  I currently have the code to populate the list for the second combo box in the GOTFOCUS event.  I seem to be stuck in a loop.  Not sure what event the code should be in.

    Tuesday, November 21, 2017 4:20 PM

Answers

  • Hi

    There are many events that may be of use to you. If you want the second CB to be populated once the user has selected a value in the first CB then use something like this:

    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.Items.AddRange({"One", "Two", "Three"})
        End Sub
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            Dim cb As ComboBox = DirectCast(sender, ComboBox)
            If cb.SelectedIndex < 0 Then Exit Sub
            ComboBox2.Items.Clear()
            Select Case cb.SelectedItem.ToString
                Case "One"
                    ComboBox2.Items.AddRange({"1", "2", "3"})
                Case "Two"
                    ComboBox2.Items.AddRange({"11", "22", "33"})
                Case Else
                    ComboBox2.Items.AddRange({"111", "222", "333"})
            End Select
        End Sub
    End Class
    if you have more specific requirements then you will need to give more details.


    Regards Les, Livingston, Scotland

    • Marked as answer by Stephen Spohn Tuesday, November 21, 2017 7:06 PM
    Tuesday, November 21, 2017 5:07 PM

All replies

  • Hi

    There are many events that may be of use to you. If you want the second CB to be populated once the user has selected a value in the first CB then use something like this:

    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.Items.AddRange({"One", "Two", "Three"})
        End Sub
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            Dim cb As ComboBox = DirectCast(sender, ComboBox)
            If cb.SelectedIndex < 0 Then Exit Sub
            ComboBox2.Items.Clear()
            Select Case cb.SelectedItem.ToString
                Case "One"
                    ComboBox2.Items.AddRange({"1", "2", "3"})
                Case "Two"
                    ComboBox2.Items.AddRange({"11", "22", "33"})
                Case Else
                    ComboBox2.Items.AddRange({"111", "222", "333"})
            End Select
        End Sub
    End Class
    if you have more specific requirements then you will need to give more details.


    Regards Les, Livingston, Scotland

    • Marked as answer by Stephen Spohn Tuesday, November 21, 2017 7:06 PM
    Tuesday, November 21, 2017 5:07 PM
  • Hello,

    Doing this properly requires absolutely no events to populate the second ComboBox. The proper way is to load a DataSet with two related tables. Set the DataRelaton between the two tables then setup two BindingSource components. A demo using database tables can be found here. But getting data from a database is not required as indicated above, use related data populated into a DataSet.

    Below is from the demo mentioned above where I'm doing three ListBox yet this could be three ComboBox controls.  In each ListBox I set the DataSouce and DisplayMember, no events required.

    EDIT If you would like the same thing w/o using a database and DataSet I can slap together something using classes. Just built a working solution with classes in C# (easier for me to prototype in C#) but can translate to VB.NET if interested.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Tuesday, November 21, 2017 5:21 PM
    Moderator
  • With no disrespect but instead trying to be educational (and perhaps I should not even say this) I never understand why one would work with events to accomplish this other than lack of knowledge or simply taking an easy way out.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim ops = New MockUpData()
    
            cboTeacher.DataSource = ops.TeachersBindingSource
            cboTeacher.DisplayMember = "Name"
            cboStudents.DataSource = ops.StudentsBindingSource
            cboStudents.DisplayMember = "Name"
    
        End Sub
    End Class
    
    

    In a class file

    Public Class Teacher
        Private _students As New List(Of Student)()
        Public Property Name() As String
        Public Property [Class]() As String
        Public ReadOnly Property Students() As List(Of Student)
            Get
                Return _students
            End Get
        End Property
    End Class
    Public Class Student
            Public Property Name() As String
            Public Property Age() As Integer
        End Class
        Public Class MockUpData
        Public Function GetData() As List(Of Teacher)
            Dim teachers As New List(Of Teacher)()
            Dim teacher As New Teacher()
            teacher.Name = "Mr. Smith"
            teacher.Class = "A1"
            teachers.Add(teacher)
    
            Dim student As New Student()
            student.Name = "Jimmy Jones"
            student.Age = 6
            teacher.Students.Add(student)
    
            student = New Student()
            student.Name = "Jane Doe"
            student.Age = 5
            teacher.Students.Add(student)
    
            teacher = New Teacher()
            teacher.Name = "Ms. Allen"
            teacher.Class = "B3"
            teachers.Add(teacher)
    
            student = New Student()
            student.Name = "Sally Student"
            student.Age = 7
            teacher.Students.Add(student)
    
            Return teachers
    
        End Function
        Public Property TeachersBindingSource As BindingSource
        Public Property StudentsBindingSource As BindingSource
        Public Sub New()
    
            TeachersBindingSource = New BindingSource
            StudentsBindingSource = New BindingSource
    
            TeachersBindingSource.DataSource = GetData()
            StudentsBindingSource.DataSource = TeachersBindingSource
            StudentsBindingSource.DataMember = "Students"
    
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, November 21, 2017 7:41 PM
    Moderator
  • With no disrespect but instead trying to be educational (and perhaps I should not even say this) I never understand why one would work with events to accomplish this other than lack of knowledge or simply taking an easy way out.

    Hi

    Hold on Karen, that is a bit harsh isn't it!

    Each to their own, everyone has a learning curve to tackle.

    Is your suggested code a better way then?


    Regards Les, Livingston, Scotland

    Tuesday, November 21, 2017 7:46 PM
  • With no disrespect but instead trying to be educational (and perhaps I should not even say this) I never understand why one would work with events to accomplish this other than lack of knowledge or simply taking an easy way out.

    Hi

    Hold on Karen, that is a bit harsh isn't it!

    Each to their own, everyone has a learning curve to tackle.

    Is your suggested code a better way then?


    Regards Les, Livingston, Scotland

    I was not trying to be harsh but instead educate. Over the years this question has come up many times and in the past two or so weeks a handful of times.

    In this situation of cascading/master-details ComboBox (or ListBox) type of question sure you can use events to do what has been asked yet it's better to not use events but instead in the majority of time this is needed data-binding with relations is the best way to go be it desktop or web base solutions.

    Is my solution better? It all depends on who looks at mine and a event driven method along with their expertise with developing solutions for a particular platform (desktop or web).


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, November 21, 2017 8:33 PM
    Moderator
  • In this situation of cascading/master-details ComboBox (or ListBox) type of question sure you can use events to do what has been asked yet it's better to not use events but instead in the majority of time this is needed data-binding with relations is the best way to go be it desktop or web base solutions.

    Databinding is an unnecessary complication for this task.  It conceals the underlying operation from the user and provides no insight into what is actually going on in the background, which is, in fact, events driving code.  I appreciate that you are not trying to educate, but Windows is an event-driven environment and exercises in things such as connected combo boxes are an excellent way to demonstrate how the events are tied together to control the flow of data.   It's not possible to learn what is happening when the code that is doing the work is hidden behind the sort of high-level functionality that data binding provides.

    Tuesday, November 21, 2017 10:01 PM