how to detect if letters are entered in textbox

how do i detect if letters are entered in a textbox?
i have this so far
1
2
if (!Char::IsDigit(e->KeyChar) && e->KeyChar != 0x08)
			e->Handled = true;

but this disables all keys except numbers. I need ctrl + v to work too. if my clipboard (ctrl +v) has letters in them, i want the textbox to detect the letters and delete them immediately.
Last edited on
Another way to attack the problem is to subclass the edit control. That procedure requires a call to SetWindowLongPtr() and is somewhat involved but very powerful. What you essentially end up with is access to the edit control's internal Window Procedure, where you have access to the keypresses directly.
ctrl v, paste, should work just fine on a text box that is set to 'number' style; make sure the style settings allow pasting? Did you try the 'number' setting, or is that setting a thing of the past?


im still confused. is there a simple way to like if textbox->text has the following characters inside "abcdefghijklmnopqrstuvwxyz" delete the characters?
Nothing is ever simple when it comes to user input. However, what you are trying to do is emminently doable. The problem can be attacked on many levels, each with its pros and cons.

As others have suggested (and I forgot about it), edit controls have a style setting I believe to only allow the input of numbers. You will have to look that up. There are various style settings for edit controls that affect their behavior.

By the way, what I and others are telling you relates more likely to SDK Api programming. If you are doing some framework that wraps the low level code that could affect things.

The other way I suggested earlier is to subclass the edit control. That is possibly the most powerful way to go about it but perhaps also the most difficult. With that technique you have access to every user interaction with the edit control, because you have access to the raw keystrokes or whatever before the internal Window Procedure of the edit control does. You can essentially 'eat' the message, i.e., if its a letter, not allow it to show up in the edit control at all. You could type letters all day 'till the cows come home and nothing will show up.

Lastly, you could allow the user to type whatever he/she wants to in the edit control, then retrieve the contents as a string of characters, and parse them. Parsing strings is a big topic in itself. In my C++ coding I use my own String Class, and one of its members removes (its actually String::Remove(....)) whatever characters you tell it to remove from a string. The C++ String Class in the Standard Library surely has something like that if you would look it up.

someone told me about the idea of slitting the strings. would that work?
Why don't you use a Spinner, HScrollBar or Slider to get numerical input instead of TextBox ?
i want the user to input numbers instead of using a slider. anyways i got this solved.
Topic archived. No new replies allowed.