[MFC] lost trying to retrieve text from an editBox
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: [MFC] lost trying to retrieve text from an editBox

  1. #1
    Join Date
    Apr 2010
    Location
    UK
    Posts
    149

    [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!

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  3. #3
    Join Date
    Apr 2010
    Location
    UK
    Posts
    149

    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!

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

    Code:
    LPTSTR bL;
    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.

  5. #5
    Join Date
    Apr 2010
    Location
    UK
    Posts
    149

    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!

  6. #6
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  7. #7
    Join Date
    Apr 2010
    Location
    UK
    Posts
    149

    Re: [MFC] lost trying to retrieve text from an editBox

    Cool! thanks very much for that!

  8. #8
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    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
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center