How to get text from CEdit control
Asked Answered
C

2

6

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.

Cheryllches answered 29/1, 2013 at 3:32 Comment(3)
I by pass this problem by use the window API directly. Use the ::GetWindowText(m_txtInput.m_hwnd, buffer, size) instead of call the method GetWindowText from the control directly, then it works. But I still want to know why previous methods can't pass the assert. Thanks.Cheryllches
It seems that the result of ::GetWindowText is empty... Why?...Cheryllches
The problem is founded, the assert problem is cause by the GetInput() method is called after the dialog closed. After the OK button is clicked, the relative window is release. Thanks.Cheryllches
A
7

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.

Antisocial answered 29/1, 2013 at 7:24 Comment(5)
One ATL class is added into my project and I include the header file #include <atlcontrols.h> to reference the ATLControls.Cheryllches
The problem is solved, it's my fault to call the GetInput method after the dialog is closed. There is no window exist after that, so GetWindowText is not working. Thank you all the same. :)Cheryllches
atlcontrols.h is not a standard ATL file/classes, it's someone's extensions.Antisocial
These text are copied from the header of atlcontrols.h: // ATLControls.h : Helper classes for common controls // // These classes are only intended as a sample and // are NOT supported by Microsoft // // This is a part of the Active Template Library. // Copyright (C) 1996-1998 Microsoft Corporation // All rights reserved. // // This source code is only intended as a supplement to the // Active Template Library Reference and related // electronic documentation provided with the library. // See these sources for detailed information regarding the // Active Template Library product.Cheryllches
it was a sample abandoned 10+ years ago, while ATL is still a supported part of the product in good standing - note the differenceAntisocial
C
1

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; 
Coagulase answered 29/3, 2017 at 1:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.