|
-
May 4th, 2010, 04:36 AM
#1
[MFC] lost trying to retrieve text from an editBox
Dear All,
I am struggling trying to understand how to retrieve text from an edit box. My view class has the following variables associated to an edit box and a static label:
CEdit inputEditBox;
CStatic flippedNameStaticVar;
I have also set a method associated to the text change in the edit box:
afx_msg void OnEnChangeInputeditbox();
Now, I would like to perform the "simple" task of showing the input provided in the edit box into the static label (flippedNameStatiVar).
Unfortunately I am lost..
I am using Visual Studio 2008 Professional and tried the following:
void CFlippedNameTreeView::OnEnChangeInputeditbox()
{
LPTSTR bufferStr = L"text displayed";
int bufferLenght = this->inputEditBox.GetWindowTextLengthW();
this->inputEditBox.GetWindowTextW(bufferStr,bufferLenght);
this->flippedNameStaticVar.SetWindowTextW(bufferStr);
}
Unfortunately the text that is displayed is "text displayed" and not the text typed by the user in the frame where the edit box is.
Even more, I am not able to access to the GetWindowText function which I read is sometimes used (the VS code helper doesn't show this as option and if I try to write it then there are compilation errors saying that is not defined).
I then tried to see wheter the buffer of the edit box text was equal to zero but it wasn't. I tried to display the content of the buffer but here again, I am struggling..
I added the following to the code above which I found in another forum:
CString ls;
ls.Format("%d",bufferLenght);
LPTSTR bL;
test = ls.GetBuffer(ls.GetLength);
this->flippedNameStaticVar.SetWindowTextW(bL);
but this time there are compilation errors:
error C2664: 'void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [3]' to 'const wchar_t *'
I am aware that I need to study more MFC but could anyone give me some insight on this?
Thanks a lot!
-
May 4th, 2010, 04:55 AM
#2
Re: [MFC] lost trying to retrieve text from an editBox
Stop using character pointers (LPTSTR). You are using C++/MFC (not C) so only use CString objects.
Code:
void CFlippedNameTreeView::OnEnChangeInputeditbox()
{
CString clString;
inputEditBox.GetWindowText (clString);
flippedNameStaticVar.SetWindowText (clString);
}
Also, you are using _W functions (SetWindowTextW). The compiler chooses the _A or _W variant of each function for you. If you define UNICODE the compiler uses the _W functions, if you don't define UNICODE, the compiler uses the _A functions. You don't need to do that yourself.
-
May 4th, 2010, 05:17 AM
#3
Re: [MFC] lost trying to retrieve text from an editBox
...thanks for that! It works..
I cannot understand why VS doesn't give me the option.. when I type
inputEditBox.GetWindowText
only the following options are available:
inputEditBox.GetWindowTextW
inputEditBox.GetWindowTextLenghtW
but if I add the code u suggested it works.. (not sure why it was giving me compilation errors before, but nevermide about this, I had probably written something else stupide somewhere..).
thanks!
-
May 4th, 2010, 05:22 AM
#4
Re: [MFC] lost trying to retrieve text from an editBox
not sure why it was giving me compilation errors before
Because of the _W functions.
LPTSTR is for the _A functions. LPWSTR is a char pointer for the _W functions. if you don't use _A or _W combined with CString, the compiler fixes all your problems based on the UNICODE compile switch.
-
May 4th, 2010, 05:25 AM
#5
Re: [MFC] lost trying to retrieve text from an editBox
thanks for that! Is really useful for me to understand..
could I ask u one last question? Why VS doesn't show me the GetWindowText option?
thanks!
-
May 4th, 2010, 06:03 AM
#6
Re: [MFC] lost trying to retrieve text from an editBox
Intellisense is responsible for showing you the options. Microsoft isn't very good in building a Intellisense system . Sometimes is gets lost. The studio studio stores this information in a file (don't know which one exactly) in your project directory. Removing this file will trigger a full rescan. This sometimes fixes the problem.
-
May 4th, 2010, 06:12 AM
#7
Re: [MFC] lost trying to retrieve text from an editBox
Cool! thanks very much for that!
-
May 4th, 2010, 10:24 AM
#8
Re: [MFC] lost trying to retrieve text from an editBox
Code:
CString ls;
ls.Format("%d",bufferLenght);
In case you get hit by this problem again in future, you better write it this way:
Code:
CString ls;
ls.Format( _T("%d"),bufferLenght);
// - OR- (If project is always Unicode (See 'L')
ls.Format(L"%d",bufferLenght);
You may like to read this:What is TCHAR
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|