Results 1 to 2 of 2

Thread: [RESOLVED] Choosing between multiple comboboxes

  1. #1

    Thread Starter
    Lively Member RichardKnox's Avatar
    Join Date
    Jul 2009
    Location
    Southern Michigan
    Posts
    89

    Resolved [RESOLVED] Choosing between multiple comboboxes

    Hello one more,

    I have a program issue I am trying to work on. I have multiple comboboxes
    (12 total) on a form.

    I am attempting to update some settings from what ever the user selects from the combo boxes.

    I am trying to keep the code as small as possible, so I thought about having all the comboboxes have similar names only different by a number being changed at the end. (ex: cbo_1, cbo_2, cbo_3, etc...)

    And using a For next loop to advance through each combobox and update the settings as the for next loop advances.

    (This might be a stupid way of doing it, but its the first idea that came to mind.)

    [CODE]
    Public Sub btn_Update_Click(etc....)

    For i = 1 to 12 <--- Loops 12 times


    My.Settings.Item("Setting_" & i) = cbo_ & i.SelectedItem

    Next

    Any help would be greatly appreceated.
    Thanks,
    Richard -
    --------
    -->Newbie Coder<<--
    Using VB.NET 2008/.NET 2.0/3.5

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Choosing between multiple comboboxes

    There are 2 ways to go about that (maybe more, but 2 general ways).

    One is to create an array of these comboboxes and you can loop through them. Like this:
    Code:
            Dim myCombos() As ComboBox = {ComboBox1, ComboBox2, ComboBox3}
            For i As Integer = 1 To 12
    
                My.Settings.Item("Setting_" & i) = myCombos(i).SelectedItem
    
            Next
    the other is to use the forms controls property to access the controls by a string name.
    Code:
            For i As Integer = 1 To 12
    
                My.Settings.Item("Setting_" & i) = DirectCast(Me.Controls("cbo_" & i.ToString), ComboBox).SelectedItem
    
            Next
    note if the combos are not directly on the form, but in a container like panel or groupbox, you would use that containers name instead of the "Me" keyword which is a reference to the form.

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