简体   繁体   English

如何在文本框包含浮点值时获取MFC中文本框的值

[英]How to get the value of a text box in MFC when the text box contains a float value

I am doing a program in MFC and I have come up with an issue. 我在MFC做一个程序,我想出了一个问题。 I have a text box created by using the toolbars in Visual Studio. 我有一个使用Visual Studio中的工具栏创建的文本框。

My text box ID is IDC_TEXT1 and I need to get the value from the text box ( float value not string ). 我的文本框ID是IDC_TEXT1 ,我需要从文本框中获取值( float不是 string )。 How do I do it? 我该怎么做?

You can get the value of by calling GetDlgItemText and std::stod like in this example: 您可以通过在此示例中调用GetDlgItemTextstd::stod来获取值:

class CAboutDlg : public CDialogEx
{
    CString m_txt;
    double m_num;
public:
    CAboutDlg() noexcept;

// Dialog Data
#ifdef AFX_DESIGN_TIME
    enum { IDD = IDD_ABOUTBOX };
#endif

protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnBnClickedOk();
};

CAboutDlg::CAboutDlg() noexcept : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
    ON_BN_CLICKED(IDOK, &CAboutDlg::OnBnClickedOk)
END_MESSAGE_MAP()

// App command to run the dialog
void CSO55114372App::OnAppAbout()
{
    CAboutDlg aboutDlg;
    aboutDlg.DoModal();
}

// CSO55114372App customization load/save methods

void CSO55114372App::PreLoadState()
{
    BOOL bNameValid;
    CString strName;
    bNameValid = strName.LoadString(IDS_EDIT_MENU);
    ASSERT(bNameValid);
    GetContextMenuManager()->AddMenu(strName, IDR_POPUP_EDIT);
    bNameValid = strName.LoadString(IDS_EXPLORER);
    ASSERT(bNameValid);
    GetContextMenuManager()->AddMenu(strName, IDR_POPUP_EXPLORER);
}

void CSO55114372App::LoadCustomState()
{
}

void CSO55114372App::SaveCustomState()
{
}

// CSO55114372App message handlers

void CAboutDlg::OnBnClickedOk()
{
    GetDlgItemText(IDC_EDIT2, m_txt);
    std::wstring s((LPCTSTR)m_txt);
    m_num = std::stod(s);
    CDialogEx::OnOK();
}

I just wanted to show you a second way of mapping a edit control to a float value. 我只想向您展示将编辑控件映射到float值的第二种方法 This is by using the ClassWizard . 这是通过使用ClassWizard

  • Right-click the control and choose Add Variable... : 右键单击该控件,然后选择Add Variable ...

添加变量

  • Set the variable category as value , the access to private , specify the name and finally set the variable type to float . 将变量类别设置为访问 private ,指定名称 ,最后将变量类型设置为float

设为浮动

  • Click Next (if required) and set the numeric range for input. 单击“ 下一步” (如果需要)并设置输入的数值范围

数字范围

  • Click Finish 单击完成

Look at the DoDataExchange method. 查看DoDataExchange方法。 It will look something like this: 它看起来像这样:

void CMFCApplication2Dlg::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_EDIT1, m_fValue);
    DDV_MinMaxFloat(pDX, m_fValue, 1, 100);
}

Whenever you need to work with the current value of the edit control, you type UpdataData(TRUE); 每当您需要使用编辑控件的当前值时,您键入UpdataData(TRUE); This synchronises the variable with the contents of the edit control . 这使变量编辑控件的内容同步。 An example with a break-point in debug mode: 调试模式中具有断点的示例:

结果

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

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