Tuesday, December 16, 2008

[vb6] Disabling user input in combobox

If you don't want the user to be able to modify or delete text in the textbox part of a combobox control, you can set its style to 2 - dropdown list. However, in doing so, you also disable your own ability to do the same, because the text property is read-only at runtime. This means that if the style is set to 2 you cannot set the text property in code and you cannot link the combo box to a database either, because scrolling the database alters the textbox property. The solution is simple: Set the style to 0 - dropdown combo, its locked property to false and add the following to the combobox code:
    Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode <> vbKeyUp And KeyCode <> vbKeyDown Then
Combo1.Locked = True
End If
End Sub

' The up-down arrow keys must be allowed to enable
' mouse-less users to scroll the list.
' Unlock the combobox again on key up events:
Private Sub Combo1_KeyUp(KeyCode As Integer, Shift As Integer)
Combo1.Locked = False
End Sub

0 comments:

Post a Comment