Solved

Getting text from a one line CEdit Control

Posted on 1998-07-13
7
241 Views
Last Modified: 2013-11-20
How do you get the text out of a CEdit Control?  I have tried using GetWindowText but that always returns NULL.
I am using add printer wizard and am trying to get the name of the printer on about the 5th page  I've used spy++ and have been able to get a handle to the CEdit window, then I  called Getline off of that, and it works in Debug mode but not release.  Any ideas on how to solve this so I can get the text

Thanks
0
Comment
Question by:curtisn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 2

Expert Comment

by:jstolan
ID: 1319088
It's not obvious is it?  I struggled for over half a day the first time I had to do this.  The answer is

CString cszBuffer;
int iCnt = m_ctrlEditShow.GetLine(0,cszBuffer.GetBuffer(10),10);
cszBuffer.ReleaseBuffer();

In this case I knew I never needed more than 10 characters, you may need to change that slightly
0
 
LVL 2

Expert Comment

by:jstolan
ID: 1319089
I should add that I used the GetDlgItem function to attach the CEdit m_ctrlEditShow member variable to the Edit box of interest.  I'd be very careful using spy to get the handle, I think that will vary from run to run.
0
 
LVL 7

Expert Comment

by:psdavis
ID: 1319090
My favorite answer is to assign a CString member variable to it and call UpdateData( TRUE ) to retrieve the data.

Phillip

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:curtisn
ID: 1319091
Now for the tricky part, I don't have the resource for this edit control.  I get the handle to some unknown window and there is an edit control in it, I've tried the method from jstolan but I can't get that to work.  Can I assign the CString to it if I don't own it?
0
 
LVL 7

Expert Comment

by:psdavis
ID: 1319092
Absolutely.  If you can get a CWnd* to it, and you know it's a CEdit, then cast it to a CEdit*.

Phillip

0
 
LVL 2

Accepted Solution

by:
warmcat earned 100 total points
ID: 1319093
Sometimes you have to go behind the back of MFC :)  Remember that under the hood it's an old-stype Windows API Edit Control as well as a potential attached CEdit.

Try using SendMessage to the edit control handle with WM_GETTEXT.  Definition of WM_GETTEXT:

An application sends a WM_GETTEXT message to copy the text that corresponds to a window into a buffer provided by the caller.

WM_GETTEXT
wParam = (WPARAM) cchTextMax;   // number of characters to copy
lParam = (LPARAM) lpszText;     // address of buffer for text
 
Parameters
cchTextMax

Value of wParam. Specifies the maximum number of characters to be copied, including the terminating null character.

lpszText

Value of lParam. Points to the buffer that is to receive the text.

Return Values
The return value is the number of characters copied.

Default Action
The DefWindowProc function copies the text associated with the window into the specified buffer and returns the number of characters copied.

Remarks
For an edit control, the text to be copied is the content of the edit control. For a combo box, the text is the content of the edit control (or static-text) portion of the combo box. For a button, the text is the button name. For other windows, the text is the window title. To copy the text of an item in a list box, an application can use the LB_GETTEXT message.

When the WM_GETTEXT message is sent to a static control with the SS_ICON style, the handle of the icon will be returned in the first four bytes of the buffer pointed to by lpszText. This is true only if the WM_SETTEXT message has been used to set the icon.


0
 

Author Comment

by:curtisn
ID: 1319094
Thanks that was what I needed, I tried all those other ways but they just wouldn't work, I'll have to remember to go in the back side of MFC

curtis
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question