ErrorCapture




Function SetComboText(MyCombo as ComboBox, MyItem as String) as Integer
Dim I as Integer
For I = 0 to MyCombo.ListCount - 1
If MyCombo.List(I) = MyItem Then
SetComboText = I
Exit Function
End If
Next I
' If the program reaches this point, the string is not in the

' list.

SetComboText = - 1
End Function
In VB, the Text property of a Combo box whose Style
property is set to '2 - Dropdown List' is read-only.
This means that a statement like
MyCombo.Text = "The Third Item"
will return an error if "The Third Item" is not part of
the list. Wouldn't it be nice if VB just set the Combo box's
ListIndex property to -1 (blanking it out) instead of bombing
out? Well, here's some code that will do just that:
Use the function like this:
AnyCombo.ListIndex = SetComboText(AnyCombo, "Any String")
If "Any String" is in the list, then the combo box's
ListIndex will be set to the correct index; if not, it will
be blanked out. The great thing about this code is that if
you want to do something else other than blanking out the
combo box, all you have to do is replace the line
SetComboText = - 1 with whatever you wish.










( errorcapture.html )- by Paolo Puglisi - Modifica del 17/12/2023