How to get text from CEdit control

五迷三道 提交于 2019-12-21 01:18:12

问题


I'm a new guy with ATL. So forgive me to ask this question.

Problem description: One CEdit control is added into a ATL dialog class. It's attached in the dialog initialize function.

//Define the edit control
ATLControls::CEdit  m_txtInput;

//In the OnInitDialog function
m_txtInput.Attach(GetDlgItem(IDC_INPUT_LINE));

m_txtInput.SetWindowText(_T("New directory"));

//In the public memeber function of the dialog GetInput()
//I have tried three kinds of method to get the text. But all of them are throw an 
//assert exception, IsWindow() failed. 
//1.
GetDlgItemText(IDC_INPUT_LINE, input);
//2.
ZeroMemory(m_lptstrInput, MAX_PATH);
m_txtInput.GetLine(0, m_lptstrInput, MAX_PATH);
//3.
BSTR input; 
m_txtInput.GetWindowText(input);

Here is a topic about how to get text from CEdit but it is not working.

Why the CEdit control could be set text with the function SetWindowText() but can't get the text by the function GetWindowText()? It's really confuse me. Thanks a lot if someone could explain it for me.


回答1:


CEdit is not an ATL class. Where the namespace ATLControls comes from? There is a WTL class with this name and getting text from it is easy:

    ATLASSERT(Edit.IsWindow()); // Make sure the control holds a handle
    CString sWindowText;
    Edit.GetWindowText(sWindowText);

The method GetWindowText is coming from ATL however and wraps GetWindowTextLength and GetWindowText API. The latter MSDN article also has a code snippet showing typical usage.

Since you mention that IsWindow does not work for you, the most likely problem is that your edit control wrapper class variable just does not have a handle of a real control, and hence getting text from nothing is impossible.




回答2:


This has been tested with MFC & VS2015:

//
// Get char string/CString from CEdit m_ceDate;
// where
// DDX_Control(pDX, IDC_EDIT_DATE, m_ceDate);

char cdateBuf[128];
UINT nCountOfCharacters = GetDlgItemText(IDC_EDIT_DATE, cdateBuf, 16);
CString csDate = cdateBuf; 


来源:https://stackoverflow.com/questions/14575228/how-to-get-text-from-cedit-control

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!