CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2006
    Posts
    285

    [RESOLVED] MessageBox syntax

    Hi
    I wanna ask something about MessageBox... I want my application bring a message but with some value will take from edit..
    For example:

    MessageBox(NULL,"The values is:"+Edit->Value)
    How the MessageBox Syntax will be?? I ask for the syntax..Do i use to need + Value or &&???

  2. #2
    Join Date
    Oct 2005
    Posts
    199

    Re: MessageBox syntax

    Make a variable, add up the text in there and print that in MessageBox.

    e.g.
    Code:
    std::string sText = "The value is: ";
    sText += Edit->Value;
    MessageBox(NULL, sText.c_str(), "", 0);
    'You help me, and I, in turn, am helped by you!'
    -H.S.

  3. #3
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: MessageBox syntax

    Do you want to add the string of the edit box to the end of the message?

    MessageBox takes 4 arguments:
    Code:
    MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpTitle, UINT uType)
    You can use GetWindowText to get the edit control's text(or you can use GetDlgItemtext if its in a dialog). Then either use strings or use strcat to append the string to the end. But you must use the string's member c_str() to pass to the message box function.

    Example:
    Code:
    // You can assign the edit's text to this
    std::string s2 = "appending";
    
    MessageBox(NULL, std::string("The value is: " + s2).c_str(), "", MB_OK);
    If you use strings you can just use the '+' operator to concatenate the strings. If you use c style strings, you can't do that you need to use strcat, sprintf, or one of those functions.

    BTW are you using MFC or a subclassed edit control? Because Edit is not a class in Win32.
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  4. #4
    Join Date
    Jul 2006
    Posts
    285

    Re: MessageBox syntax

    I think i am ok with all these... Thanks.. Sorry i am new what you mean MFC or BTW? Also what c_str() does exactly? So if i wanna bring this message:

    "My Value Is" 5 for example i need strcat and sprintf because i mess integer with strings?

    Also what is that o your code? std:
    Last edited by Leite33; December 12th, 2008 at 01:12 AM.

  5. #5
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: MessageBox syntax

    Well MFC is basically Win32 put in classes. I was thrown off by Edit->Text since the '->' operator is used to get access to a class' function or member. In Win32 an edit box is created by calling CreateWindow with "EDIT" as the class name parameter. And BTW is just short for by the way.

    the std:: is the namespace that string belongs to. You could put after you include the file string(#include <string> there is no .h for C++ strings header) the code "using namespace std;" which would allow you to omit it, but if you don't you need to specify the namespace it is contained in which is std(which is for standard namespace).

    .c_str() just cast's the C++ string to a C style string so you can pass it to the functions that take in C style strings.

    You can us strcat if you have a string containing the text of the edit box and a string containing the text "My Value Is: " or whatever, then you can use strcat to combine the two strings together. For example if you text box contains the number 20 and you store that in it's own string, we'll call the string variable szEditText, and you have another string containing the text "You Number Is: ", which we'll call this string szStaticText, you have two strings, now you would want to combine them, so you call strcat:
    Code:
    char szStaticText[20] = "My Value Is: ";
    char szEditText[3] = "";
    // Get the text of the edit control, assume it holds the value 20
    GetWindowText(hWndEdit, szEditText, 3);
    strcat(szStaticText, szEditText);
    now szStaticText is "My Value Is: 20"
    To use strcat you must include the file string.h(#include <string.h> notice this one does have a .h extension)

    For sprintf you can use format specifiers. For sprintf you include the file stdio.h. You have a destination string, or the string that will hold the formatted string. Then you specify what you want in the formatted string, and a variable list of arguments depending on the format specifiers.
    Code:
    char szStaticText[20] = "My Value Is: ";
    char szEditText[3] = "";
    GetWindowText(hWndEdit, szEditText, 3);
    sprintf(szStaticText, "%s%s", szStaticText, szEditText);
    The %s is a specifier signaling that it expects a string variable. This has two so the last two parameters should be strings.
    And that will output "My Value Is: 20" for the szStaticText just like strcat. If you just have an int value you can use the specifier %d and make that variable an int instead of the %s and string:
    Code:
    char szStaticText[20] = "My Value Is: ";
    int nNumber = 20; 
    sprintf(szStaticText, "%s%d", szStaticText, nNumber);
    I would highly suggest that if you don't know the uses of these functions that you read up on them, there are plenty of great resources available. cplusplus.com Reference is a great start.

    Regards
    NotSoSuperHero
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  6. #6
    Join Date
    Jul 2006
    Posts
    285

    Re: MessageBox syntax

    Thanks a lot....

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured