none
How to put the text from a string variable, into a messagebox, in VS Express 2012 C++?

    Question

  • The following example program compiles and runs as expected:

    -------------------------------------

    #include "stdafx.h"

    #include "windows.h"

    #include <string>

    #include <iostream>

    using namespace std;

    /* int _tmain(int argc, _TCHAR* argv[])  */

    int main()

    {

          int II;

          string stg1;   

                stg1="Here's the first string";

          /* cout << stg1 << endl << endl ; */

          II=MessageBox (NULL, TEXT("First Message Box."), TEXT ("HelloMsg"), 3);

          return 0;

    }

    ----------------------------------------------

    It puts up a small message box with "HelloMsg" on the top bar, and "First Message Box." in the middle, without the double-quote marks of course; and puts in three buttons.

    But if I replace the "First Message Box." with the variable name stg1, it fails to compile.

    Can a string variable not be used in a message box this way?

    Is there a way to put the text from the string variable (instead of a constant string), in a message box (or other such box)?

    Thanks!

    Wednesday, November 14, 2012 8:19 PM

Answers

  • The following example program compiles and runs as expected:
     

    #include "stdafx.h"
     
    #include "windows.h"
     
    #include <string>
     
    #include <iostream>
     
    using namespace std;
     
    /* int _tmain(int argc, _TCHAR* argv[]) */
     
    int main()
     
    {
     
          int II;
     
          string stg1;
     
                stg1="Here's the first string";
     
          /* cout << stg1 << endl << endl ; */
     
          II=MessageBox (NULL, TEXT("First Message Box."), TEXT ("HelloMsg"), 3);
     
          return 0;
     
    }
     

    It puts up a small message box with "HelloMsg" on the top bar, and "First Message Box." in the middle, without the double-quote marks of course; and puts in three buttons.
     
    But if I replace the "First Message Box." with the variable name stg1, it fails to compile.
     
    Can a string variable not be used in a message box this way?
     
    Is there a way to put the text from the string variable (instead of a constant string), in a message box (or other such box)?
                   

    The Windows API does not know anything about std::string.

    In a non-Unicode build you can do

    II=MessageBox (NULL, stg1.c_str(), "HelloMsg", 3);

    but this will not work in Unicode build.

    The Microsoft way is to use CString (which knows about Unicode settings, and also has a const TCHAR* conversion operator):

    CString stg1 = _T("Here's the first string)";
    II=MessageBox (NULL, stg1, _T("HelloMsg"), 3);

    If you want to use std::string/wstring then a useful trick is

    typedef std::basic_string<TCHAR> tstring;

    tstring stg1 = _T("Here's the first string)";
    II=MessageBox (NULL, stg1.c_str(), _T("HelloMsg"), 3);


    David Wilkinson | Visual C++ MVP

    Wednesday, November 14, 2012 8:34 PM
  • I figured the first line ("Input file has been opened") might be written to some kind of string variable (followed by a \n) which is placed in the box. Later the second line ("145 lines read from input file") is appended to the string variable, with its own \n at the end, and the string with both lines will appear in the box. Later the third line ("Input file has been closed") would be appended to the string variable with its own \n, and all three lines will appear in the box. etc. etc.

    Here's an expanded example with the string growing:

    #include "stdafx.h"
    #include "windows.h"
    #include <string>
    
    int main()
    {
      int II;
      typedef std::basic_string<TCHAR> tstring;
      tstring stg1 = _T("Here's the first string");
      II=MessageBox (NULL, stg1.c_str(), 
          TEXT ("HelloMsg"), 3);
    
      stg1 += _T("\r\nHere's the 2nd part!\r\n");
      II=MessageBox (NULL, stg1.c_str(), 
          TEXT ("HelloMsg"), 3);
    
      stg1 += _T("And here's the 3rd part!\r\n");
      II=MessageBox (NULL, stg1.c_str(), 
          TEXT ("HelloMsg"), 3);
    
        return 0;
    }
    

    - Wayne

    Wednesday, November 14, 2012 11:17 PM

All replies

  • The following example program compiles and runs as expected:
     

    #include "stdafx.h"
     
    #include "windows.h"
     
    #include <string>
     
    #include <iostream>
     
    using namespace std;
     
    /* int _tmain(int argc, _TCHAR* argv[]) */
     
    int main()
     
    {
     
          int II;
     
          string stg1;
     
                stg1="Here's the first string";
     
          /* cout << stg1 << endl << endl ; */
     
          II=MessageBox (NULL, TEXT("First Message Box."), TEXT ("HelloMsg"), 3);
     
          return 0;
     
    }
     

    It puts up a small message box with "HelloMsg" on the top bar, and "First Message Box." in the middle, without the double-quote marks of course; and puts in three buttons.
     
    But if I replace the "First Message Box." with the variable name stg1, it fails to compile.
     
    Can a string variable not be used in a message box this way?
     
    Is there a way to put the text from the string variable (instead of a constant string), in a message box (or other such box)?
                   

    The Windows API does not know anything about std::string.

    In a non-Unicode build you can do

    II=MessageBox (NULL, stg1.c_str(), "HelloMsg", 3);

    but this will not work in Unicode build.

    The Microsoft way is to use CString (which knows about Unicode settings, and also has a const TCHAR* conversion operator):

    CString stg1 = _T("Here's the first string)";
    II=MessageBox (NULL, stg1, _T("HelloMsg"), 3);

    If you want to use std::string/wstring then a useful trick is

    typedef std::basic_string<TCHAR> tstring;

    tstring stg1 = _T("Here's the first string)";
    II=MessageBox (NULL, stg1.c_str(), _T("HelloMsg"), 3);


    David Wilkinson | Visual C++ MVP

    Wednesday, November 14, 2012 8:34 PM
  • Thank you, David!

    Is there an additional #include statement I need to use, to make this Cstring declaration work?

    Wednesday, November 14, 2012 9:34 PM
  • Is there an additional #include statement I need to use, to make this Cstring declaration work?

    Note that it's CString not Cstring.

    It's part of MFC, which isn't included with the Express
    Editions of VC++. Since you're apparently using VC++ 2012
    Express, you probably don't have it and ergo can't use it.

    - Wayne

    Wednesday, November 14, 2012 9:51 PM
  •  

    CString stg1 = _T("Here's the first string)";

    tstring stg1 = _T("Here's the first string)";


    David Wilkinson | Visual C++ MVP

    Typos in these two lines: the closing quotes and braces
    are transposed. Should be:

    CString stg1 = _T("Here's the first string");

    tstring stg1 = _T("Here's the first string");

    - Wayne

    Wednesday, November 14, 2012 10:04 PM
  • Is there an additional #include statement I need to use, to make this Cstring declaration work?

    Note that it's CString not Cstring.

    It's part of MFC, which isn't included with the Express
    Editions of VC++. Since you're apparently using VC++ 2012
    Express, you probably don't have it and ergo can't use it.

    - Wayne

    Hmm, I guess that's the problem.

    Is there any other kind of dialog box I can use to do this?

    Basically I'm writing a program that will do a great many things. As it runs, I want it to (first) put up a window called "Status window". Then later as the program runs, it will occasionally write text to that window, describing what's going on in the program, such as "Input file has been opened", followed later by "145 lines read from input file", and later by "Input file has been closed", etc. etc.

    I figured the first line ("Input file has been opened") might be written to some kind of string variable (followed by a \n) which is placed in the box. Later the second line ("145 lines read from input file") is appended to the string variable, with its own \n at the end, and the string with both lines will appear in the box. Later the third line ("Input file has been closed") would be appended to the string variable with its own \n, and all three lines will appear in the box. etc. etc.

    Perhaps a MessageBox isn't a good way to do this. Is there any other way in VS 2012 Express C++, to put up and write to such a window?

    Wednesday, November 14, 2012 10:50 PM
  • Actually, I guess a much simpler question that will fill the bill is:

    How can I get the contents of a string variable, to appear in a window such as a Message Box or other kind of dialog box?

    Wednesday, November 14, 2012 11:01 PM
  • How can I get the contents of a string variable, to appear in a window such as a Message Box or other kind of dialog box?

    Dave showed you one way:

    #include "stdafx.h"
    #include "windows.h"
    #include <string>
    
    int main()
    {
      int II;
      typedef std::basic_string<TCHAR> tstring;
      tstring stg1 = _T("Here's the first string");
      II=MessageBox (NULL, stg1.c_str(), 
          TEXT ("HelloMsg"), 3);
    
      return 0;
    }
    

    - Wayne

    Wednesday, November 14, 2012 11:08 PM
  • I figured the first line ("Input file has been opened") might be written to some kind of string variable (followed by a \n) which is placed in the box. Later the second line ("145 lines read from input file") is appended to the string variable, with its own \n at the end, and the string with both lines will appear in the box. Later the third line ("Input file has been closed") would be appended to the string variable with its own \n, and all three lines will appear in the box. etc. etc.

    Here's an expanded example with the string growing:

    #include "stdafx.h"
    #include "windows.h"
    #include <string>
    
    int main()
    {
      int II;
      typedef std::basic_string<TCHAR> tstring;
      tstring stg1 = _T("Here's the first string");
      II=MessageBox (NULL, stg1.c_str(), 
          TEXT ("HelloMsg"), 3);
    
      stg1 += _T("\r\nHere's the 2nd part!\r\n");
      II=MessageBox (NULL, stg1.c_str(), 
          TEXT ("HelloMsg"), 3);
    
      stg1 += _T("And here's the 3rd part!\r\n");
      II=MessageBox (NULL, stg1.c_str(), 
          TEXT ("HelloMsg"), 3);
    
        return 0;
    }
    

    - Wayne

    Wednesday, November 14, 2012 11:17 PM
  • Wayne, VERY helpful, thank you! It works!

    Only problem is, I get the first message box with just the first part in it (as expected). I click one of the buttons and it vanishes.

    Then a second box appears with the first and second parts in it. I click one of the buttons and that one vanishes.

    Then a third box appears with the first, second, and third parts in it. I click a button and that one vanishes.

    Is there a way to make the box appear only once, and stay there as the program runs? And, some time later, the second part appears in that original box just after the first part? And then sometime after that, the third part appears in the original box just after the second?

    Perhaps a message box is not the way to do this, but some other kind of box is needed?

    Wednesday, November 14, 2012 11:40 PM
  • >> Perhaps a message box is not the way to do this, but some other kind of box is needed?

    A dialog with a list box on it would be a good choice.

    Wednesday, November 14, 2012 11:45 PM
  • Is there a way to make the box appear only once, and stay there as the program runs? And, some time later, the second part appears in that original box just after the first part? And then sometime after that, the third part appears in the original box just after the second?

    Perhaps a message box is not the way to do this, but some other kind of box is needed?

    You're looking for the type of functionality that requires an
    event-driven design. A console mode program such as you have
    been using isn't well suited to that type of activity. It
    generally needs a message pump, message handler, etc. In
    other words, a Win32 GUI or .NET (WinForms) application,
    etc.

    A console application is best suited to linear logic flow.

    A MessageBox stops the program from proceeding until you
    click one of the "buttons", or Escape, Exit, etc. It is
    closed/destroyed automatically once a response is received
    from it.

    - Wayne

    Thursday, November 15, 2012 12:13 AM