none
Populating textboxes based on comboboxes selection RRS feed

  • Question

  • I need to populate 18 textboxes based on what i choose in 3 comboboxes, so the first combobox has a device then the second cbox has a deivce model and the last has a firmware version, what i need is based on these three combinations i need the textboxes to show different codes that i have how can i do that and what code do i need to use?
    Saturday, September 16, 2017 10:55 AM

All replies

  • Hi

    Please show an example of the  'different codes that i have', and the criteria you use to decide how to fill the TextBoxes.


    Regards Les, Livingston, Scotland

    Saturday, September 16, 2017 11:12 AM
  • I need to populate 18 textboxes based on what i choose in 3 comboboxes, so the first combobox has a device then the second cbox has a deivce model and the last has a firmware version, what i need is based on these three combinations i need the textboxes to show different codes that i have how can i do that and what code do i need to use?

    I'd suggest that you start with a class and a collection of those class instances. The class also needs to be how the second and third comboboxes are populated: Dynamically.

    Think about it: You want to make sure that all combinations that the user could choose don't result in "nothing matches". To do that, have the class take care of it - with LINQ for example.

    Something I've shown here before works like this: The first combo would be where the user would select a state name (U.S. states). Once chosen, the second combo would have all cities that are in that state and once a city is chosen, the third combo would have all zip codes that are in that city and state.

    The class has the information to do that. It'll take some setting up but I think that's the best approach.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, September 16, 2017 11:17 AM
  • Hello,

    Please indicate where the information flows from e.g. a database (and if so, what is the database SQL-Server, MS-Access etc) or another source. If coming from a database you can use tables with relationships ties together with one or more BindingSource components. If not from a database this can still apply or go with an alternate route as Frank is indicating.


    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

    Saturday, September 16, 2017 11:49 AM
    Moderator
  • As a kind of sample,

    http://www.vb-tips.com/ComboboxRelatedDataViewDistinct.ASPX

    However, this is not such a simple sample as you probably think. 

    It is very depending from where the data comes (I did this once with WMI)


    Success
    Cor


    • Edited by Cor Ligthert Saturday, September 16, 2017 2:55 PM
    Saturday, September 16, 2017 2:54 PM
  • I need to populate 18 textboxes based on what i choose in 3 comboboxes, so the first combobox has a device then the second cbox has a deivce model and the last has a firmware version, what i need is based on these three combinations i need the textboxes to show different codes that i have how can i do that and what code do i need to use?

    I thought I'd put something together to give you the idea of what I meant. As Karen pointed out, it doesn't matter where the data comes from (although it'll change how you get the data) -- the concept is the same.

    You can repeat this as a test on your end just to get a feel for it.

    Start a new project and put three ComboBoxes and a PictureBox on the form. So long as you name the controls the same as mine, the code that I'll show shortly will work.

    Name the controls as follows:

    combo_Make
    combo_Classification
    combo_Model
    PictureBox1

    I have mine set up like this:

    Now go to code view and please replace all of yours with the following:

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
        Private _autos As Automobiles
    
    
    
        Private Sub Form1_Load(sender As System.Object, _
                               e As System.EventArgs) _
                               Handles MyBase.Load
    
            _autos = New Automobiles
    
            With combo_Make
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.ListItems
                .Items.AddRange(_autos.GetMakes)
            End With
    
            With combo_Classification
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.ListItems
                .Enabled = False
            End With
    
            With combo_Model
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.ListItems
                .Enabled = False
            End With
    
            With PictureBox1
                .BorderStyle = BorderStyle.None
                .SizeMode = PictureBoxSizeMode.Zoom
            End With
    
        End Sub
    
    
    
        Private Sub _
            combo_Make_SelectedIndexChanged(sender As System.Object, _
                                            e As System.EventArgs) _
                                            Handles combo_Make.SelectedIndexChanged
    
            If combo_Make.SelectedIndex > -1 Then
                With PictureBox1
                    If .Image IsNot Nothing Then
                        .Image.Dispose()
                        .Image = Nothing
                    End If
                End With
    
                With combo_Classification
                    .SelectedIndex = -1
                    .Items.Clear()
                    .Enabled = False
                End With
    
                With combo_Model
                    .SelectedIndex = -1
                    .Items.Clear()
                    .Enabled = False
                End With
    
                Dim make As String = combo_Make.Items(combo_Make.SelectedIndex).ToString
    
                With combo_Classification
                    .Items.AddRange(_autos.GetClassifications(make))
    
                    If .Items.Count > 0 Then
                        .Enabled = True
                    End If
    
                    If .Items.Count = 1 Then
                        .SelectedIndex = 0
                    End If
                End With
            End If
    
        End Sub
    
    
    
        Private Sub _
            combo_Classification_SelectedIndexChanged(sender As System.Object, _
                                                      e As System.EventArgs) _
                                                      Handles combo_Classification.SelectedIndexChanged
    
            If combo_Make.SelectedIndex > -1 AndAlso combo_Classification.SelectedIndex > -1 Then
                With PictureBox1
                    If .Image IsNot Nothing Then
                        .Image.Dispose()
                        .Image = Nothing
                    End If
                End With
    
                With combo_Model
                    .SelectedIndex = -1
                    .Items.Clear()
                    .Enabled = False
                End With
    
                Dim make As String = combo_Make.Items(combo_Make.SelectedIndex).ToString
                Dim classification As String = combo_Classification.Items(combo_Classification.SelectedIndex).ToString
    
                Dim classificationEnum As Automobiles.AutoType = _
                    DirectCast([Enum].Parse(GetType(Automobiles.AutoType), classification), Automobiles.AutoType)
    
                With combo_Model
                    .Items.AddRange(_autos.GetModels(make, classificationEnum))
    
                    If .Items.Count > 0 Then
                        .Enabled = True
                    End If
    
                    If .Items.Count = 1 Then
                        .SelectedIndex = 0
                    End If
                End With
            End If
    
        End Sub
    
    
    
        Private Sub _
            combo_Model_SelectedIndexChanged(sender As System.Object, _
                                             e As System.EventArgs) _
                                             Handles combo_Model.SelectedIndexChanged
    
            If combo_Model.SelectedIndex > -1 Then
                Dim make As String = combo_Make.Items(combo_Make.SelectedIndex).ToString
                Dim classification As String = combo_Classification.Items(combo_Classification.SelectedIndex).ToString
    
                Dim classificationEnum As Automobiles.AutoType = _
                    DirectCast([Enum].Parse(GetType(Automobiles.AutoType), classification), Automobiles.AutoType)
    
                Dim model As String = combo_Model.Items(combo_Model.SelectedIndex).ToString
                Dim url As String = _autos.GetImageURL(make, classificationEnum, model)
    
                If Not String.IsNullOrWhiteSpace(url) Then
                    PictureBox1.ImageLocation = url
                End If
            End If
    
        End Sub
    End Class
    
    
    
    
    
    Public Class Automobiles
        Public Enum AutoType
            NA
            Car
            SUV
        End Enum
    
        Private _make As String
        Private _classification As AutoType
        Private _model As String
        Private _imageURL As String
    
        Private _data As IEnumerable(Of Automobiles)
    
        Public Sub New()
    
            Dim tempList As New List(Of Automobiles)
            InitializeData(tempList)
            _data = tempList.ToArray
    
        End Sub
    
        Private Sub New(ByVal make As String, _
                        ByVal autoType As AutoType, _
                        ByVal model As String, _
                        ByVal url As String)
    
            _make = make.Trim
            _classification = autoType
            _model = model.Trim
            _imageURL = url.Trim
    
        End Sub
    
        Public Function GetMakes() As String()
            Return (From a As Automobiles In _data Select a.Make Distinct Order By Make).ToArray
        End Function
    
        Public Function GetClassifications(ByVal make As String) As String()
    
            If Not String.IsNullOrWhiteSpace(make) Then
                Return (From a As Automobiles In _data Where a.Make = make Select a.Classification_String Distinct Order By Classification_String).ToArray
            Else
                Return New String() {}
            End If
    
        End Function
    
        Public Function GetModels(ByVal make As String, _
                                  ByVal classification As AutoType) As String()
    
            If Not String.IsNullOrWhiteSpace(make) AndAlso Not classification = AutoType.NA Then
                Return (From a As Automobiles In _data Where a.Make = make AndAlso a.Classification = classification Select a.Model Order By Model).ToArray
            Else
                Return New String() {}
            End If
    
        End Function
    
        Public Function _
            GetImageURL(ByVal make As String, _
                        ByVal classification As AutoType, _
                        ByVal model As String) As String
    
            If Not String.IsNullOrWhiteSpace(make) AndAlso Not classification = AutoType.NA AndAlso Not String.IsNullOrWhiteSpace(model) Then
                Dim qry As System.Collections.Generic.IEnumerable(Of Automobiles) = _
                    From a As Automobiles In _data _
                        Where a.Make = make AndAlso _
                              a.Classification = classification AndAlso _
                              a.Model = model
    
                If qry.Count = 1 Then
                    Return qry.First.ImageURL
                Else
                    Return Nothing
                End If
            Else
                Return Nothing
            End If
    
        End Function
    
        Public ReadOnly Property Classification As AutoType
            Get
                Return _classification
            End Get
        End Property
    
        Public ReadOnly Property Classification_String As String
            Get
                Return _classification.ToString
            End Get
        End Property
    
        Public ReadOnly Property ImageURL As String
            Get
                Return _imageURL
            End Get
        End Property
    
        Public ReadOnly Property Make As String
            Get
                Return _make
            End Get
        End Property
    
        Public ReadOnly Property Model As String
            Get
                Return _model
            End Get
        End Property
    
        Private Sub InitializeData(ByVal seList As List(Of Automobiles))
    
            If seList IsNot Nothing Then
                With seList
                    .Clear()
                    .TrimExcess()
    
                    .Add(New Automobiles("Alfa Romeo", AutoType.Car, "4C", _
                                               "http://media.ed.edmunds-media.com/alfa-romeo/4c/2017/oem/2017_alfa-romeo_4c_coupe_base_fq_oem_1_717.jpg"))
    
                    .Add(New Automobiles("Alfa Romeo", AutoType.Car, "Giulia", _
                                               "http://media.ed.edmunds-media.com/alfa-romeo/giulia/2017/oem/2017_alfa-romeo_giulia_sedan_base_fq_oem_8_717.jpg"))
    
                    .Add(New Automobiles("Alfa Romeo", AutoType.SUV, "Stelvio", _
                                               "http://media.ed.edmunds-media.com/alfa-romeo/stelvio/2018/oem/2018_alfa-romeo_stelvio_4dr-suv_ti_fq_oem_3_717.jpg"))
    
                    .Add(New Automobiles("Ferrari", AutoType.Car, "458 Italia", _
                                               "http://media.ed.edmunds-media.com/ferrari/458-italia/2012/oem/2012_ferrari_458-italia_coupe_base_fq_oem_2_717.jpg"))
    
                    .Add(New Automobiles("Ferrari", AutoType.Car, "488 GTB", _
                                               "http://media.ed.edmunds-media.com/ferrari/488-gtb/2016/oem/2016_ferrari_488-gtb_coupe_base_fq_oem_7_717.jpg"))
    
                    .Add(New Automobiles("Ferrari", AutoType.Car, "California T", _
                                              "http://media.ed.edmunds-media.com/ferrari/california-t/2016/oem/2016_ferrari_california-t_convertible_base_fq_oem_2_717.jpg"))
    
                    .Add(New Automobiles("Ferrari", AutoType.Car, "F12 Berlinetta", _
                                              "http://media.ed.edmunds-media.com/ferrari/f12-berlinetta/2016/oem/2016_ferrari_f12-berlinetta_coupe_base_fq_oem_1_717.jpg"))
    
                    .Add(New Automobiles("Lamborghini", AutoType.Car, "Aventador", _
                                             "http://media.ed.edmunds-media.com/lamborghini/aventador/2017/oem/2017_lamborghini_aventador_convertible_lp-700-4-roadster_fq_oem_5_717.jpg"))
    
                    .Add(New Automobiles("Lamborghini", AutoType.Car, "Huracan", _
                                             "http://media.ed.edmunds-media.com/lamborghini/huracan/2017/oem/2017_lamborghini_huracan_convertible_lp-580-2-spyder_fq_oem_2_717.jpg"))
    
                    .Add(New Automobiles("Maserati", AutoType.Car, "Ghibli", _
                                             "http://media.ed.edmunds-media.com/maserati/ghibli/2017/oem/2017_maserati_ghibli_sedan_s-q4_fq_oem_3_717.jpg"))
    
                    .Add(New Automobiles("Maserati", AutoType.Car, "GranTurismo", _
                                            "http://media.ed.edmunds-media.com/maserati/granturismo/2017/oem/2017_maserati_granturismo_coupe_sport_fq_oem_1_717.jpg"))
    
                    .Add(New Automobiles("Maserati", AutoType.Car, "Quattroporte", _
                                            "http://media.ed.edmunds-media.com/maserati/quattroporte/2017/oem/2017_maserati_quattroporte_sedan_gts-granlusso_fq_oem_2_717.jpg"))
    
                    .Add(New Automobiles("Maserati", AutoType.SUV, "Levante", _
                                            "http://media.ed.edmunds-media.com/maserati/levante/2017/oem/2017_maserati_levante_4dr-suv_s_fq_oem_2_717.jpg"))
    
                    .Add(New Automobiles("McLaren", AutoType.Car, "570GT", _
                                            "http://media.ed.edmunds-media.com/mclaren/570gt/2017/oem/2017_mclaren_570gt_coupe_base_fq_oem_6_717.jpg"))
    
                    .Add(New Automobiles("McLaren", AutoType.Car, "570S", _
                                          "http://media.ed.edmunds-media.com/mclaren/570s/2017/oem/2017_mclaren_570s_coupe_base_fq_oem_1_717.jpg"))
    
                    .Add(New Automobiles("McLaren", AutoType.Car, "675LT Coupe", _
                                         "http://media.ed.edmunds-media.com/mclaren/675lt-coupe/2016/oem/2016_mclaren_675lt-coupe_coupe_base_fq_oem_1_717.jpg"))
                End With
            End If
    
        End Sub
    End Class

    Right - just some nice low-cost family cars like you can see there. ;-)

    Notice a few things in the code please:

    First, anytime a combo is changed, all of the dependent combos are cleared and disabled. I do this because the information currently in those combos (if any) is no longer applicable as soon as you change the criteria.

    Secondly, if there's only one to choose from (which is the case with several of these), I have the code go ahead and select it. After all, it's not much of a choice if there's only one. ;-)

    When you run it, you'll see what I mean so I hope that you'll take a few minutes and try it out:

    It "cascaded through", depending on what was chosen before it. In all cases, whatever can be selected will produce a result and I feel sure that's what you want. To continue, I'll now change the classification to "SUV". There's only one in that classification so it's automatically selected. That in turn sets the image to the associated URL:

    Take a few minutes and set it up, if nothing but just so you can get an idea of what I meant:

    I hope it helps. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Sunday, September 17, 2017 10:22 PM
  • I need to populate 18 textboxes based on what i choose in 3 comboboxes, so the first combobox has a device then the second cbox has a deivce model and the last has a firmware version, what i need is based on these three combinations i need the textboxes to show different codes that i have how can i do that and what code do i need to use?

    Hi Skellytone,

    According to your description, you want to get some data from database based on your three combobox and populate 18 textbox, am I right? if yes, please tell me you use what kinds of database? You can also refer to the same issue firstly.

    https://stackoverflow.com/questions/42901578/automatically-fill-text-boxes-based-on-a-combo-box-selection-in-vb-net

    https://stackoverflow.com/questions/27882682/vb-net-filling-textbox-on-combobox-selected-index-changed-with-sql-database

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 18, 2017 7:25 AM
    Moderator