Simple ComboBox Question with tough answer?

CanOz

Newcomer
Joined
Apr 2, 2005
Messages
7
What I want to do is fairly simple. (or so I thought.)

I have 2 ComboBoxes on my Form each populated with the same data. I want to check when the user Selects an item in the second ComboBox and see if it is the same as the item they selected in the first ComboBox. If they are different then they can proceed, however if they are the same then they must choose a different item in the second ComboBox.

Here is a generic version of the code I am using in the SelectedIndexChanged Event of the second ComboBox.

If ComboBox2.Text = "Select a Colour" Then
Exit Sub
End If

If ComboBox2.Text = ComboBox1.Text Then
If MessageBox.Show("You must select a different Colour", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error) = DialogResult.OK Then
ComboBox2.Text = "Select a Colour"
Exit Sub
End If

' code here if the two selected items do not match

Now the hitch. The Text in the second ComboBox does not change to what I want, it actually reverts back to the Text of the Item selected.

Can anyone tell me how to go about setting the Text of a ComboBox?
 
What do you have the .DropDownStyle property set to?

If it is set to DropDownList then you can not set the .Text property; it will ignore you. That's because a DropDownList is meant to select (and display) only items from a preset list, which I'm guessing does not include "Select a Colour", or even "Select a Color". You could include "Select a Colour" as the first item in the Items collection, and instead of [ComboBox2.Text = "Select a Colour"] you could do [ComboBox.SelectedIndex = 0]. The drawback to this, however, is that when the user drops down the list, "Select a Colour" will be the first item listed. Otherwise, it will work exactly as you'de like.

Now... if the .DropDownStyle property is not set to DropDownList, then I don't know what the problem is, but I'm sorry for wasting your time.
 
marble_eater said:
Now... if the .DropDownStyle property is not set to DropDownList, then I don't know what the problem is, but I'm sorry for wasting your time.

Hi

Never a waste of time to ask.

The ComboBoxes are set to DropDown and the Text is not in the List of Items. For some reason, I seem to be unable to set the Text Property of the ComboBox using the SelectedIndexChanged Event of the ComboBox. I can change the Text Property of other Controls, just not the one that the event is for.

I am probably being overly fussy with this as the User would not be able to continue until the condition of having different items selected is met, but I like to prompt my users just that little bit more.
 
The reason that it seems you can't change the text is that the SelectedIndexChanged event is raised BEFORE the combo box actually changes the .Text property to match the selected item. That is, the user selects and item from the drop down list, the SelectedIndexChanged event is raised, you handle the event and set the text, and then, after you handle the event, the combobox sets its text to what the user selected, overwriting the text you set.

This is the behavior I observed by throwing MessageBoxes on about every line of code to get a play-by-play. You will have to find a workaround.
 
marble_eater said:
You will have to find a workaround.
I was afraid of that. I really don't like the idea that I must write extra code to do this silly little thing that should be handled with one line of code.

Right now I am using a Hidden TextBox to cover the ComboBox if the Items match, but it just does not look quite right. That could just be me being VERY picky though.

Thanks for your help and suggestions.
 
if the two comboboxes are both populated with the same set of data, then
if combobox1.selectedindex = comboxbox2.selectedindex then
... they match
else
..they don't
end

if the order in which the items appear is different in the comboboxes, you'll need to try
if combobox1.selectedindex = combobox1.findstring(combobox2.selecteditem.text)

to see if the text selected in combobox2 exists in combobox1's items.
 
Back
Top