allanephillips
asked on
How can I get a WinForms text control's text string, given a pointer to its CWnd.
I have a Winforms app whose window contains several controls. From a separate MFC process I need to be able to retrieve the value of one of the text controls in the Winforms app.
I enumerate those controls to get a CWnd pointer for each one. That all works fine.
The window has several pushbuttons and several text controls. On each pushbutton, when I call GetWindowText() I get the pushbutton's caption returned. As expected.
However when I call GetWindowText() on a text object I get nothing back even though there is clearly a non-null string there.
I'm assuming that one cannot use GetWindowText() on a Winforms text object but I don't know what I can use that would give me back the text that's stored within a text object.
I enumerate those controls to get a CWnd pointer for each one. That all works fine.
The window has several pushbuttons and several text controls. On each pushbutton, when I call GetWindowText() I get the pushbutton's caption returned. As expected.
However when I call GetWindowText() on a text object I get nothing back even though there is clearly a non-null string there.
I'm assuming that one cannot use GetWindowText() on a Winforms text object but I don't know what I can use that would give me back the text that's stored within a text object.
I think GetWindowText should work fine. Can you post some code, there could be a problem. Also, can you check that the windows class of your edit controls is a correct one. It should have a value similar to "WindowsForms10.EDIT.app.0 .34f5582_r 9_ad1" (ignore the last bit, it is always different). It must have "EDIT" in it
Please note that for normal Winform Edit controls the EDIT window will be a child of the form. If the edit is a toggle EDIT (with up and down arrows) then it will be part of a composite control and the actual EDIT control will be one level below. Check the spy++ for the well know WinForms program below:
ASKER
Well the window class that I seem to be seeing is: "WindowsForms10.Window.8.a pp.0.202c6 66". It sounds as though that is not the right one.
When I use spy++ I don't see any descendant window that has ".EDIT" in its class name so it may be that the window that spy++ shows that looks like an edit control really isn't one.
When I use spy++ I don't see any descendant window that has ".EDIT" in its class name so it may be that the window that spy++ shows that looks like an edit control really isn't one.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
when using spy, you could "search" for the control by dragging the search icon of spy to the text control.
try to get the window handle of the parent window and the id of the text control. then you can do:
note, the windows form uses utf-16 character set which is called UNICODE by MS. the mfc application might use 'multibyte character set' where the TCHAR is a single char. if that is the case and the GetWindowText returns a positive length but the text seems to be empty, you may try to using WCHAR instead of TCHAR and call the ::GetWindowTextL which could handle wide strings.
Sara
try to get the window handle of the parent window and the id of the text control. then you can do:
HWND hwndTextCtrl == ::GetDlgItem(hwndParent, id);
TCHAR text[1024] = { 0 };
int len = ::GetWindowText(hwndTextCtrl, text, 1024);
note, the windows form uses utf-16 character set which is called UNICODE by MS. the mfc application might use 'multibyte character set' where the TCHAR is a single char. if that is the case and the GetWindowText returns a positive length but the text seems to be empty, you may try to using WCHAR instead of TCHAR and call the ::GetWindowTextL which could handle wide strings.
Sara
ASKER
The solution really just told me that there is likely no way to get at the data in what's probably a "Custom Control". I guess I'll have to try to get the data using an OCR converter.