简体   繁体   English

如何从编辑控件中获取数值

[英]how to get numeric value from edit control

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. MFC编辑由CEdit类表示的控件。

Thank you. 谢谢。

Besides the GetWindowText method already mentioned, you can also bind it via DDX to an integer/unsigned integer/double/float value. 除了已经提到的GetWindowText方法之外,您还可以通过DDX将其绑定到整数/无符号整数/双/浮点值。 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. 而m_iNumber是您的CYourAwesomeDialog类的成员。

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. 在重新阅读我的答案后,我注意到UpdateData(...)需要一个BOOL变量 - 已更正。 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来使它更具可读性(就像在stdafx.h或某个中心头文件中):

enum UpdateDataDirection
{
    FromVariablesToControls = FALSE,
    FromControlsToVariables = TRUE
}

and you would just have to write: 你只需要写:

UpdateData(FromVariablesToControls);

or 要么

UpdateData(FromControlsToVariables);

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: CEdit派生自CWnd,因此它有一个名为GetWindowText的成员函数,您可以调用它来获取CEdit中的文本,然后将其转换为数字类型, intdouble - 具体取决于您希望用户输入的内容:

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

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. 如果您需要定期需要该功能,比如多个对话框,那么您也可以将自己的CEdit派生类子类化,以进行获取,设置和验证工作。

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. 这样的事情,确保消息映射将所有其他消息传递给父CEdit。 You could extend it to have customisable lower and upper limit and decimal places setting. 您可以将其扩展为具有可自定义的下限和上限以及小数位设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM