views:

87

answers:

5

How do I get the contents of a textbox in C++?

+6  A: 

Use the Win32 API GetWindowText passing in the text box's window handle.

If you want to get the text from another process use WM_GETTEXT instead with SendMessage.

Brian R. Bondy
If in the string I had "1 + 1", do you know how I could make it do 1 + 1?
ITg
@ITg: You will probably need to parse the string into parts and then do your calculation.
Brian R. Bondy
ok, thanks for your help
ITg
+1  A: 

CWnd::GetWindowText()

John Dibling
That's MFC, not "winapi".
MSalters
+1  A: 

GetWindowText()

Abyx
A: 
//unicode std::string or std::wstring
typedef std::basic_string<TCHAR> unicode_string;

unicode_string GetWinString(HWND h)
{
int len = ::GetWindowTextLength(h);
if (len)
  {
  std::vector<TCHAR> tmp(len + 1,_T('\0'));
  ::GetWindowText(h,&tmp[0],len + 1);
  return &tmp[0];
  }
return _T("");
}
Hugh Jorgan
A: 

Correction to last post:

//unicode std::string or std::wstring
typedef std::basic_string<TCHAR> unicode_string;

unicode_string GetWinString(HWND h)
{
int len = ::GetWindowTextLength(h);
if (len)
  {
  std::vector<TCHAR> tmp(len + 1,_T('\0'));
  ::GetWindowText(h,&tmp[0],len + 1);
  return &tmp[0];
  }
return _T("");
}
Hugh Jorgan
There's an "edit" option below your posts; please don't post additional answers.
MSalters