dcsimg
Results 1 to 7 of 7

Thread: [RESOLVED] How to loop code to execute for each item in a ComboBox control

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    18

    Resolved [RESOLVED] How to loop code to execute for each item in a ComboBox control

    Hello,

    I have the following form in my application

    http://imgur.com/T7IddGO

    The Master City combobox is called "MasterCity", and the Slave City combobox is called "SlaveCity". The ListView that has the classes listed is called "Mismatch". Basically, what this form does is when you click the Check Button, it checks all classes that are scheduled for the Master City, and if it finds that the same class is not held on the same date for the Slave City, the Master city class is added to the Mismatch ListView.

    I'm currently trying to create a Check All button (seen on the right side of the prior screenshot), which would basically do the same thing as the Check button but would check ALL Slave cities at once instead of just the one Slave city currently selected in the SlaveCity combobox, but I don't know how to loop the current Check button functionality to occur for every item in the SlaveCity combobox.

    Below is the relevent code for the current function that fires when the Check button is clicked
    Code:
    Private Sub CheckForMismatches(ButtonSent As Integer)
    
       Dim SlaveCityID As Long
       Dim MasterCityID As Long
       '-Dim all the other variables needed by this functionů
    
       Me.MousePointer = vbHourglass
       Mismatch.ListItems.Clear
       MasterCityID = masterCity.ItemData(masterCity.ListIndex)
    
              '-// I'm guessing the loop would need to be started here
    
       SlaveCityID = SlaveCity.ItemData(SlaveCity.ListIndex)    '-This line gets the SlaveCityID from the ComboBox, which is an important value I need in order to identify the Slave City in SQL statements
      
       If MasterCityID >= 0 And SlaveCityID >= 0 Then
    
    	'- **All the Code that identifies classes scheduled for the Master City but NOT the Slave City is inside this If statement.  The class information is then output to the Mismatch ListView control. This is the code that needs to be looped for every  item in the SlaveCity dropdown**
    
       End If
    
              '-// The loop would probably be ended here
    
       StatusBar.Caption = "Out of " & masterCount & " classes " & Mismatch.ListItems.count & " differences found."
       If Mismatch.ListItems.count > 0 Then
          Create.Enabled = True
       End If
       
       Me.MousePointer = vbNormal
    End Sub
    So, my question is: How do I code a loop that will do the following:

    1) Get the SlaveCityID for the first item in the SlaveCity comboBox
    2) Execute all the code in that main If statement
    3) Get the next SlaveCityID for the next SlaveCity comboBox item, and run through the loop again?

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    7,309

    Re: How to loop code to execute for each item in a ComboBox control

    Well, to loop through comboboxes, you simply increase the listindex by 1, starting with 0, and loop until you get to the end (.listcount -1).
    Each time, you can get the value of the Combobox(es) to use in your IF statements by capturing the comboxbox.text value, like so
    Code:
    dim x as integer
    dim myValue as String
    for x = 0 to cbo1.listcount - 1
        cbo1.listindex = x
        myValue = cbo1.Text
        '''Call your IF stuff here
    next x

  3. #3

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    18

    Re: How to loop code to execute for each item in a ComboBox control

    Quote Originally Posted by SamOscarBrown View Post
    Well, to loop through comboboxes, you simply increase the listindex by 1, starting with 0, and loop until you get to the end (.listcount -1).
    Each time, you can get the value of the Combobox(es) to use in your IF statements by capturing the comboxbox.text value, like so
    Code:
    dim x as integer
    dim myValue as String
    for x = 0 to cbo1.listcount - 1
        cbo1.listindex = x
        myValue = cbo1.Text
        '''Call your IF stuff here
    next x
    Thank you both who have responded so far. However, I do not want the actual value displayed in the ComboBox - There is actually a "behind-the-scenes" value that is stored in each combobox item that I need (I apologize for not really making that clear). I'll explain what I mean.

    If you look at my first screenshot of the UI, you can see that the SlaveCity ComboBox is displaying a value of "NM - Las Cruces", but that combobox item also has a number ID value of "116" (in this case) that is not shown in the UI. This line in my original code example is what accesses that numeric ID:

    SlaveCityID = SlaveCity.ItemData(SlaveCity.ListIndex)

    This is the line that sets SlaveCityID = 116 (Apparently SlaveCity.ItemData is how you access that non-UI value). THAT is what needs to be looped through for each combobox item in the combobox. What's the loop I need to make to set SlaveCityID to that "behind the scenes" value for each combobox item?
    Last edited by sygg13; Dec 5th, 2013 at 11:59 AM.

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    13,084

    Re: How to loop code to execute for each item in a ComboBox control

    using example in post 3 simply change
    Code:
    myValue =cbo1.List(i)
    to
    Code:
    myValue =cbo1.ItemData(i)

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    18

    Re: How to loop code to execute for each item in a ComboBox control

    Quote Originally Posted by DataMiser View Post
    using example in post 3 simply change
    Code:
    myValue =cbo1.List(i)
    to
    Code:
    myValue =cbo1.ItemData(i)
    Ah, OK thank you. I wasn't aware of the syntax for doing that. Thanks everyone, I think I can stumble my way through from here

  7. #7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width