Link to home
Start Free TrialLog in
Avatar of ccbfSEARCA
ccbfSEARCA

asked on

Combo box dilemma

I have a combo box and I've set the style property to dropdown list and the sorted property to true. I also have a click event procedure linked with this combo box. My dilemma is when I try to place a value from an ADO recordset into the text of the combo box. When the program reaches that line of code it automatically jumps to the associated click event procedure and does an infinite loop there. I need the style property to be in dropdown list so I can prevent entries that aren't in the combo boxes choices but the dropdown list keeps making this problem with the infinite loop.
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Hi, why don't you show your codes then ppls can solve it. : )
I have no problem doing this, are you sure your code is not in the Change event, I have done that before.
You should not change the text of the Combo box, in the change event, which will trigger the change event, to put into a loop.
Avatar of gajendra
gajendra

Please show your code.

Gajendra
Hi, i know your problem.

You get a change-event every time textbox of the ComboBox cahnges its text or you click on the button.

You shouln't call a sub/function in the click-event of a combobox which changes the text of that combo (stack overflow).

If you havent another choice you could set a flag, determining whether the change was initiated from user or from code.

ASKER CERTIFIED SOLUTION
Avatar of zzconsumer
zzconsumer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
zzconsumer, firstly it is not considered good practice to lock questions by posting an answer in this way. You should have submitted your post as a comment. You do NOT have the one and only 100% definitive answer to this problem.

The "answer" posted by zzconsumer doesn't actually address the problem as I understand it. You are attempting to set the .Text property of the combo box to an item in the list, this does in fact raise the click event when you have finished. A slightly better solution to avoid the re-entrance is to include a static boolean in the event code:

Private Sub Combo1_Click()
    Static blnClicked As Boolean
    If Not blnClicked Then
        blnClicked = True
        Exit Sub
    End If
    Combo1.ListIndex = 1
    blnClicked = False
End Sub

Shows how this avoids the infinite loop.
Yeah, I know and I'm sorry for that. As I see, I marked 'answer' instead of 'comment' as I was a bit sleepy this morning.
Avatar of ccbfSEARCA

ASKER

I thought of a similar solution (the reverse of using Exit Sub) just before I read yours. It gets me through the infinite loop and many lines that are unnecessarily read but I was hoping to find a property, setting or something that will totally avoid the Click procedure (unless really clicked). Since our solutions were very similar, grading you was like grading myself. This will do for now but I have a lot of these combo boxes and there is an exponential increase in the number of lines of code that my program has to go through.

Just a thought, the following does not fire the Click event

    Combo1.Text = Combo1.List(Index)