Solved

Getting text from a one line CEdit Control

Posted on 1998-07-13
7
229 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
  • 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

679 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