13

Sorry if this is too trivial, but I can't figure out how to get numeric value entered into edit control. MFC edit control represented by CEdit class.

Thank you.

pic11
  • 14,267
  • 21
  • 83
  • 119

3 Answers3

20

Besides the GetWindowText method already mentioned, you can also bind it via DDX to an integer/unsigned integer/double/float value. Try this:

void CYourAwesomeDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_EDIT_NUMBER, m_iNumber);
}

whereas m_iNumber is a member of your CYourAwesomeDialog class.

You have to call

UpdateData(TRUE);

in order to write the values from the controls into the variables. Call

UpdateData(FALSE);

to do it the other way around - from the variables in the controls.

EDIT (Bonus):

Upon re-reading my answer, I noticed that UpdateData(...) needs a BOOL variable - corrected. So I had an idea for people who like readability. Because I always got confused which call did which direction, you could introduce an enum to make it more readable, like so (maybe in stdafx.h or some central header):

enum UpdateDataDirection
{
    FromVariablesToControls = FALSE,
    FromControlsToVariables = TRUE
}

and you would just have to write:

UpdateData(FromVariablesToControls);

or

UpdateData(FromControlsToVariables);
CppChris
  • 1,226
  • 9
  • 14
  • I like the suggestion about renaming true/false, as this is really totally confusing everytime. :) – Devolus Feb 16 '21 at 22:37
10

CEdit derives from CWnd, so it has a member function called GetWindowText which you can call to get the text in the CEdit, and then convert that into numeric type, int or double - depending on what you expect user to enter:

CString text;
editControl.GetWindowText(text);

//here text should contain the numeric value
//all you need to do is to convert it into int/double/whatever
Nawaz
  • 353,942
  • 115
  • 666
  • 851
2

If you're going to need that functionality regularly, say on multiple dialogs, then you may as well subclass your own CEdit-derived class for doing the getting, setting and validation work.

class CFloatEdit : public CEdit
{
public:
    CFloatEdit();
    void SetValue(double v) {
        // format v into a string and pass to SetWindowText
        }
    double GetValue() {
        // validate and then return atoi of GetWindowText
        }
    void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) {
        // only allow digits, period and backspace
        }
};

Something like that, make sure the message map passes along all other messages to the parent CEdit. You could extend it to have customisable lower and upper limit and decimal places setting.

acraig5075
  • 10,588
  • 3
  • 31
  • 50
  • I would use DDV for validation work - but you can pass a pointer to this control to highlight validation errors – CppChris May 09 '12 at 08:33
  • Considering CEdit already has a number mode (only allowing digits and decimal separator) it would make sense to have functions to get/set int/long/double values, instead of forcing every single developer on the earth to implement its own... – mBardos Jan 22 '20 at 08:01