roglan
asked on
Simple combobox
Is it possible to restrict a user to only the supplied values with a SIMPLE COMBOBOX? Not a dropdown combo, or dropdown list. I know it is easier to use the latter for this functionality, but I have an assignment were it is a requirement that the user should only be able to select between 4 specified values, AND it is a requirement that I use a simple combobox.....
The user should only be able to select one of these values, not edit, or enter his own value....
Hope somebody knows a smart solution
:-)
The user should only be able to select one of these values, not edit, or enter his own value....
Hope somebody knows a smart solution
:-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi roglan,
Is this whan you want:
Dim myItems() As String
Dim myPreIndex as Integer
Private sub Form_Load()
myPreIndex = -1
Redim myItems(3)
myItems(0)="Hello"
myItems(1)="World"
myItems(2)="123"
myItems(3)="abc"
combo.Additem "Hello"
combo.Additem "World"
combo.Additem "123"
combo.Additem "abc"
combo.Additem ": )"
combo.Additem "Good Luck"
combo.Additem "EE"
combo.Additem "Hope will help"
End sub
Private sub Combo_Click()
If CheckValue(combo.Text) = True Then (of course Combo's Style is 2, otherwise CheckValue(combo.List(comb o.Listinde x)) )
msgbox "Match"
myPreIndex = combo.ListIndex
else
msgbox "No Match"
combo.ListIndex = myPreIndex
end if
End Sub
Private Function CheckValue(myValue As String) As Boolean
for i = LBound(myItems) To UBound(myItems)
if myValue = myItems(i) Then CheckValue = True:Exit Function
next i
CheckValue = False
End Sub
'Hope will help.
Is this whan you want:
Dim myItems() As String
Dim myPreIndex as Integer
Private sub Form_Load()
myPreIndex = -1
Redim myItems(3)
myItems(0)="Hello"
myItems(1)="World"
myItems(2)="123"
myItems(3)="abc"
combo.Additem "Hello"
combo.Additem "World"
combo.Additem "123"
combo.Additem "abc"
combo.Additem ": )"
combo.Additem "Good Luck"
combo.Additem "EE"
combo.Additem "Hope will help"
End sub
Private sub Combo_Click()
If CheckValue(combo.Text) = True Then (of course Combo's Style is 2, otherwise CheckValue(combo.List(comb
msgbox "Match"
myPreIndex = combo.ListIndex
else
msgbox "No Match"
combo.ListIndex = myPreIndex
end if
End Sub
Private Function CheckValue(myValue As String) As Boolean
for i = LBound(myItems) To UBound(myItems)
if myValue = myItems(i) Then CheckValue = True:Exit Function
next i
CheckValue = False
End Sub
'Hope will help.
You can use something like below
Private Sub Combo1_LostFocus()
Dim i As Integer
Dim blnInList As Boolean
blnInList = False
For i = 0 To Combo1.ListCount - 1
If Combo1.List(i) = Combo1.Text Then
blnInList = True
End If
Next
If blnInList = False Then
MsgBox "You must choose an item from the list"
Combo1.SetFocus
End If
End Sub
Private Sub Combo1_LostFocus()
Dim i As Integer
Dim blnInList As Boolean
blnInList = False
For i = 0 To Combo1.ListCount - 1
If Combo1.List(i) = Combo1.Text Then
blnInList = True
End If
Next
If blnInList = False Then
MsgBox "You must choose an item from the list"
Combo1.SetFocus
End If
End Sub
Failure to accept that the proper tool should be used for a job is ludicrous!
Would you use pliers to hammer in a nail just because it was a requirement? You could--you shouldn't.
When you have a tool that does EXACTLY what is needed, it is worth fighting for it to be used properly.
--
However, if you don't use the tool properly (because your boss/customer/etc. will fire you otherwise) then ensure that you properly document what you're doing AND WHY so that the next poor soul who looks at it understands why you did this. Furthermore, since YOU don't know why, I suggest asking the person setting the requirement to explain why so you can document it as such (this takes the pressure off you because you're simply trying to do a good job!) When you explain that a feature is already available to do that, then maybe the requirement will change! If not, at least you can understand why.
Hopefully you'll get a better answer than just "because I said so", but if that's the answer, document it as such: The simple combobox was used instead of the dropdown list because Joe said to use it "because I said so." If nothing else, future programmers will get a kick out of it!
Would you use pliers to hammer in a nail just because it was a requirement? You could--you shouldn't.
When you have a tool that does EXACTLY what is needed, it is worth fighting for it to be used properly.
--
However, if you don't use the tool properly (because your boss/customer/etc. will fire you otherwise) then ensure that you properly document what you're doing AND WHY so that the next poor soul who looks at it understands why you did this. Furthermore, since YOU don't know why, I suggest asking the person setting the requirement to explain why so you can document it as such (this takes the pressure off you because you're simply trying to do a good job!) When you explain that a feature is already available to do that, then maybe the requirement will change! If not, at least you can understand why.
Hopefully you'll get a better answer than just "because I said so", but if that's the answer, document it as such: The simple combobox was used instead of the dropdown list because Joe said to use it "because I said so." If nothing else, future programmers will get a kick out of it!
Hmm a simple combo without an edit field: You mean a listbox? (you could add a separate label to show the current selection if you really want).
I think the soliton i have given should be more than fine.Just add items,Make keyascci=0 in the combp's keypressevent.select using arrow keys
If the answer is satisfactory,whatz ur problem in giving grade A.Dont always expect a fundu answerwhich can be solved in a line of code.
The soltion is for simple combo as u have mentioned.Otherwise u can use style=2