Solved

Getting text from a one line CEdit Control

Posted on 1998-07-13
7
202 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Rewrite URL With Encoded Characters 6 64
only14 challenge 19 64
EvenOdd challenge 10 83
How to convert MFC::CString to UTF8 wchar_t* 10 102
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now