Combobox with KeyPress and e.Handled

T

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
 
B

Bernie Yaeger

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
 
F

Fergus Cooney

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
 
B

Bernie Yaeger

Hi Fergus,

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

Bernie
 
F

Fergus Cooney

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
 

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