How to disable the "ESC feature" of a ComboBox?

S

Stefan Mueller

I've a UserForm with a ComboBox with e.g. 10 items.

Private Sub UserForm_Initialize()
Dim Counter As Long

For Counter = 1 To 10
ComboBox1.AddItem "MyItem " & Counter
Next

ComboBox1.ListIndex = 5
End Sub

Choose another item in the ComboBox. Now, if you press 'Esc' the
before selected item gets selected (and there is also no KeyPress
event if you press 'Esc' the first time).
How can I disable this feature? I don't want that 'Esc' selects the
before selected item. I like e.g. 'Esc' to close the UserForm.
 
G

Guest

Hi Stefan,

This should help, although it still won't work if Escape is pressed whilst
the Combobox list is still dropped down. If you Choose a list item first, or
are using the cursors to navigate up and down the list then this should
prevent the list resetting to the last item.

Try using this code in your form:

'These are variables for the form in general
Dim cboVal As Long
Dim boolEsc As Boolean
Option Explicit

Private Sub ComboBox1_Change()
If boolEsc = True Then
boolEsc = False
ComboBox1.ListIndex = cboVal
Else
cboVal = ComboBox1.ListIndex
boolEsc = False
End If
End Sub

Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
If KeyCode = 27 Then
boolEsc = True
End If
End Sub

Private Sub UserForm_Initialize()
Dim Counter As Long
boolEsc = False
For Counter = 1 To 10
ComboBox1.AddItem "MyItem " & Counter
Next
ComboBox1.ListIndex = 5
End Sub



Sean.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top