How to make an edit box accept only digits from 0 - 9 in mfc.?
-
Hi, I have a dialog box and an Edit box control on that. I want to make this edit box accept only digits ( 0 - 9 ), no character or special characters. I have changed its property "Number" to true in resource dialog but when I run the application, the screen blinks every time I press any character. Or is there any way that I can manually change it to accept only digits.? Any help will be appreciated. Regards, Mbatra
-
Hi, I have a dialog box and an Edit box control on that. I want to make this edit box accept only digits ( 0 - 9 ), no character or special characters. I have changed its property "Number" to true in resource dialog but when I run the application, the screen blinks every time I press any character. Or is there any way that I can manually change it to accept only digits.? Any help will be appreciated. Regards, Mbatra
You could overwrite the OnChange Handler (I think it was EN_CHANGED or something like that) and manually get the text, check for any non-digit and remove it. Then set the corrected Text with SetWindowText(), or ReplaceSel(). There are however several pitfalls you need to avoid. First, setting the text programmatically also calls the event handler, and you need some status variable to prevent "recursive" calls. Second, You need to take the cursor position into account. When you call SetWindowText(), usually the whole edit control content is selected. You could set the cursor position to the end with SetSel() (IIRC you need two calls, one to mark everything and at the same time set the cursor to the end, a second one to mark nothing and leave the cursor where it is.) Since the user may have entered something in the middle you may want to get the cursor position before changing anything (with GetSel()) and set it back to that position later. I usually avoided these issues by letting the user enter the wrong values and checking them when the control lost focus, unless where was a strong argument against this approach.
-
You could overwrite the OnChange Handler (I think it was EN_CHANGED or something like that) and manually get the text, check for any non-digit and remove it. Then set the corrected Text with SetWindowText(), or ReplaceSel(). There are however several pitfalls you need to avoid. First, setting the text programmatically also calls the event handler, and you need some status variable to prevent "recursive" calls. Second, You need to take the cursor position into account. When you call SetWindowText(), usually the whole edit control content is selected. You could set the cursor position to the end with SetSel() (IIRC you need two calls, one to mark everything and at the same time set the cursor to the end, a second one to mark nothing and leave the cursor where it is.) Since the user may have entered something in the middle you may want to get the cursor position before changing anything (with GetSel()) and set it back to that position later. I usually avoided these issues by letting the user enter the wrong values and checking them when the control lost focus, unless where was a strong argument against this approach.
-
Hi, I have a dialog box and an Edit box control on that. I want to make this edit box accept only digits ( 0 - 9 ), no character or special characters. I have changed its property "Number" to true in resource dialog but when I run the application, the screen blinks every time I press any character. Or is there any way that I can manually change it to accept only digits.? Any help will be appreciated. Regards, Mbatra
If using
DoDataExchange
andDDX_Text
, then you also have access toDDV_MinMaxInt
(And other DDV friends[^]) -
Hi, Here is the solution: I handled OnUpdate() function. This will not allow user to enter characters or special characters. void Position::OnUpdateEditRotate() { CString str; (static_cast(GetDlgItem(IDC_EDIT_ROTATE)))->GetWindowTextW(str); LPTSTR pBuff = str.GetBuffer( 10 ); bool bProblem = false; for ( int indx = 0; indx < str.GetLength(); indx++ ) { char nChar = pBuff[indx]; if ( ( ( nChar >= 'A' ) && ( nChar <= 'Z' ) ) || ( ( nChar >= 'a' ) && ( nChar <= 'z' ) ) || ( ( nChar >= 33 ) && ( nChar <= 47 ) ) || ( ( nChar >= 58 ) && ( nChar <= 64 ) )|| ( ( nChar >= 91 ) && ( nChar <= 96 ) )|| ( ( nChar >= 123 ) && ( nChar <= 126 ) )) { MessageBox(); } else { bProblem = true; break; } } str.ReleaseBuffer(); }
-
Hi, Here is the solution: I handled OnUpdate() function. This will not allow user to enter characters or special characters. void Position::OnUpdateEditRotate() { CString str; (static_cast(GetDlgItem(IDC_EDIT_ROTATE)))->GetWindowTextW(str); LPTSTR pBuff = str.GetBuffer( 10 ); bool bProblem = false; for ( int indx = 0; indx < str.GetLength(); indx++ ) { char nChar = pBuff[indx]; if ( ( ( nChar >= 'A' ) && ( nChar <= 'Z' ) ) || ( ( nChar >= 'a' ) && ( nChar <= 'z' ) ) || ( ( nChar >= 33 ) && ( nChar <= 47 ) ) || ( ( nChar >= 58 ) && ( nChar <= 64 ) )|| ( ( nChar >= 91 ) && ( nChar <= 96 ) )|| ( ( nChar >= 123 ) && ( nChar <= 126 ) )) { MessageBox(); } else { bProblem = true; break; } } str.ReleaseBuffer(); }
-
Thanks for sharing. It would probably work same if you check just for < numbers > range. Vaclav