Search code examples
c++windowsinputmfccedit

Is it possible to switch between numeric only and alpha-numeric input modes for a CEdit control?


I have some radio buttons that decide the type of input to be given to the CEdit control. By design, my CEdit control accepts numbers and characters.

Is there a way to limit this input to only numbers only when the user clicks on a provided radio button? Also, when the user clicks on the other option (i.e. alpha-numeric mode), is it possible to switch back?

I set up my CEdit control to accept alpha-numeric characters using Visual C++'s GUI (i.e. right click on the control -> Properties -> "Styles" tab -> select "Number").

An example use case:

  1. User starts application.
  2. User clicks on some button and this dialog with the CEdit control in consideration shows.
  3. User is presented with radio buttons where the alpha-numeric button is selected by default (since this is the default mode set at design time).
  4. User clicks on numeric.

At this point I want to restrict input to the CEdit control to only numbers. Similarly, after this, if the user clicks on the alpha-numeric radio button again, I want to remove that restriction.


Solution

  • Yes, use CEdit::ModifyStyle() to either set or clear the ES_NUMBER style.

    So, to make it accept numbers only:

    CEdit *pEdit = // get your control here ...
    pEdit->ModifyStyle(0, ES_NUMBER);
    

    And to revert to accepting all characters:

    pEdit->ModifyStyle(ES_NUMBER, 0);