By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,125 Members | 1,479 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,125 IT Pros & Developers. It's quick & easy.

Combobox with KeyPress and e.Handled

P: n/a
Tom
I have a ComboBox that I wrote a KeyPress event for. Basically, it looks to
make sure that a numeric key has been pressed; if not, then it does a Beep()
and sets e.Handled to True. This should, as I understand it, cause the
character to 'go away' and not be displayed in the box. I have tried this
on a plain old TextBox and it functions as it should. However, with a
ComboBox the character stays in the box! The code (via the debugger) works
fine - if I enter an 'a', it detects it is not a numeric, sets e.Handled =
TRUE, then exits the subroutine. But no matter what I do, the non-numeric
'a' (or whatever character) displays and stays in the combobox.

Anyone explain this? And is there a workaround? Thanks.

Tom
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi Tom,

It's a bit klugy, but this, inside the keypress event, will work:
If e.KeyChar = "a" Then

SendKeys.Send("{BACKSPACE}")

End If

HTH,

Bernie Yaeger

"Tom" <to*@nospam.com> wrote in message
news:uv**************@TK2MSFTNGP12.phx.gbl...
I have a ComboBox that I wrote a KeyPress event for. Basically, it looks to make sure that a numeric key has been pressed; if not, then it does a Beep() and sets e.Handled to True. This should, as I understand it, cause the
character to 'go away' and not be displayed in the box. I have tried this
on a plain old TextBox and it functions as it should. However, with a
ComboBox the character stays in the box! The code (via the debugger) works fine - if I enter an 'a', it detects it is not a numeric, sets e.Handled = TRUE, then exits the subroutine. But no matter what I do, the non-numeric
'a' (or whatever character) displays and stays in the combobox.

Anyone explain this? And is there a workaround? Thanks.

Tom

Nov 20 '05 #2

P: n/a
Hi Tom,

Does your ComboBox's KeyPress handler actually work?

I plonked a ComboBox on a Form and added handlers for KeyDown and
KeyPress. I put a breakpoint in each one and ran the program.

Neither of them stopped at the breakpoint.

Regards,
Fergus
Nov 20 '05 #3

P: n/a
Hi Fergus,

Mine worked fine - see my code above (even a 'messagebox.show' responded as
you would expect).

Bernie

"Fergus Cooney" <fi*****@post.com> wrote in message
news:uX**************@TK2MSFTNGP12.phx.gbl...
Hi Tom,

Does your ComboBox's KeyPress handler actually work?

I plonked a ComboBox on a Form and added handlers for KeyDown and
KeyPress. I put a breakpoint in each one and ran the program.

Neither of them stopped at the breakpoint.

Regards,
Fergus

Nov 20 '05 #4

P: n/a
Hi Tom, Bernie,

I had been experimenting with Release and Debug and was still compiled
Release versions. Lol. There ain't no breakpoints in a Release! ;-)

Unfortunately I discovered a problem with the SendKeys method. This is
what I used:
If e.KeyChar <= "0"c Or e.KeyChar >= "9"c Then
If e.KeyChar <> Chr(8) Then
SendKeys.Send ("{BACKSPACE}")
End If
End If

The inner If is to stop it sending BackSpace forever (as I found out!).
But when I typed Ctrl-C into the editbox, all hell broke loose, but I don't
know why.

Tom, have you had any joy yet?

Regards,
Fergus
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.