PDA

View Full Version : Synchronize two combo boxes



entwined
02-08-2012, 07:59 PM
Hi VB'ers,

I have a form that has two comboboxes and I want to make them synchronized. Say ComboBox1 and ComboBox2 are the names of the comboboxes. They'll always have the same list count but different list content. Sample is:

ComboBox1's items are "Color1", "Color2", "Color3", "Color4"
ComboBox2's items are "Red", "Blue", "Green", "Yellow"

Both have 4 items right. I want to match the two comboboxes so when I select ComboBox1 to "Color1", I want ComboBox2 to automatically display "Red". If ComboBox1 is "Color2", ComboBox2 should display "Blue" and so on and vice versa.

Need help please. I hope I made it clear for you guys but tell me if something is not. Thanks... :)

fumei
02-08-2012, 09:04 PM
But they are comboboxes. They will "display" their lists. ComboBox2 will always display "Red", "Blue", "Green", "Yellow". What is the purpose here?

Are you sure you want two comboboxes? You could have a combobox and a textbox, so that if ComboBox1 is SELECTED with "Color1" then the textbox displays "Red".

What are you trying to achieve?

entwined
02-08-2012, 09:54 PM
fumei,

Yes. Both are comboboxes I am sure of it! I just want them to be synchronized and make the two always match according to that order upon selecting an item in either of the comboboxes.

Both of the combobox have lists. I have provided an example of just simple colors.

Don't ask for the purpose. I just want it that way. :)

fumei
02-08-2012, 11:08 PM
But I did ask the purpose.

OK, hopefully someone else can help you. As I stated, as they ARE comboboxes, they will have the lists you give them. If Combobox2 has "Red", "Blue", "Green", "Yellow", then that is what it has. Regardless of what is selected in ComboBox1, it will STILL have "Red", "Blue", "Green", "Yellow".

So what is there to match? Do you mean if you SELECT something in ComboBox1, then focus is shifted to Combobox2 and something SELECTED? That is the best "match" you can come up with. It is possible.

And yeah, I can...and do..ask the purpose. The reason? I may be able to suggest something that could work easier and better. Your call.

fumei
02-09-2012, 12:10 AM
Do you mean somnething like this?

entwined
02-09-2012, 12:27 AM
Yes! You nailed it man! Hehehe. I'm sorry if I somehow wasn't clear with my instructions. Yup, this should be it. The Comboboxes are synchronized.

The file's VB project is password protected though :( Could you unlock it or give me the code please. Thank you so much!.......

fumei
02-09-2012, 01:21 AM
So satisfy my curiosity. Why do you want to do this?

entwined
02-09-2012, 02:18 AM
OK. My project is actually this. I have two combo boxes. ComboBox1 contains just list of ID's (e.g. URL_ID.001, URL_ID.002, ... and so on) and ComboBox2 contains list of URL's/web address. The idea is when I change either of the combobox, it will automatically access the selected URL.

I just provided a sample of just colors before for simpler data.

You were probably thinking that I was kind of rude before? I'm sorry about that. I'm just in a hurry a while ago. :)

Hope you can help me this time. If not, that's OK.....

defcon_3
02-09-2012, 03:26 AM
It looks like something like this in both comboBox:)


Private Sub ComboBox1_Change()
Dim equiv As Integer
Dim switch As Integer
If switch = 0 Then
switch = switch + 1
Else
equiv = ComboBox1.ListIndex
ComboBox2.ListIndex = equiv
End If

fumei
02-09-2012, 09:22 AM
Yup, that is it. Except I did the two variables as Public (in a standard module):

Option Explicit

Public mCount As Integer
Public var As Integer


And then the following in the userform module:

Private Sub ComboBox1_Change()
If mCount = 0 Then
mCount = mCount + 1
Else
var = ComboBox1.ListIndex
ComboBox2.ListIndex = var
End If
End Sub


Private Sub ComboBox2_Change()
If mCount = 0 Then
mCount = mCount + 1
Else
var = ComboBox2.ListIndex
ComboBox1.ListIndex = var
End If
End Sub


The reason you have the IF test of 0 is that the Change event fires when you first Show the userform - a useless event in this case.

entwined
02-09-2012, 03:10 PM
This should be it. Works great! I thank you so much sir!... :)

Anne Troy
02-09-2012, 05:38 PM
Jerry, for some reason I have thought about you a half dozen times this week. Then I run into this thread. Hm. Are you looking for any side work? If yes, email me anne dot troy at gmail
xxxooo