[英]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: 您可以通过在此示例中调用
GetDlgItemText
和std::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 。
float
. float
。 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.