VC++ Express Messagebox::Show() Coding Question

Hello all,

I am trying to take the contents of a textbox, and display it as part of a custom message on a message box. I can display either my message, or the contents, but I'm having trouble getting both. I searched the help, but couldn't find an example. Thanks for any help. Here is my code (with explanatory comments):

{
// This code is from the button1_Click event. Clicking the button should take the
// text from a textbox and display "You entered <insert textbox text here>"

// Create a sting, assign contents of textBox1
String^ msgboxInput = textBox1->Text;
        
// I need equiv. of printf("You entered %s", msgboxInput) to display in message
// box, but I can't figure out how to combine "You entered " and msgboxInput.
// I can display either, but not both.
MessageBox::Show( "You entered ", "Thanks for testing this box!",
MessageBoxButtons::OK, MessageBoxIcon::Exclamation );
< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />



Answer this question

VC++ Express Messagebox::Show() Coding Question

  • chisanga

    Okay, I guess my explanation could have been better. The printf was only to demonstrate how I wanted to insert my variable the same way printf does, but for display on the message box.

    But I tried it dynamically, and it works:

    String^ msgboxInput = "You entered " + textBox1->Text;
    Messagebox::Show( msgboxInput );

    Also:

    String^ msgboxInput = textBox1->Text;
    String^ msgboxOutput = You entered " + msgboxInput;
    Messagebox::Show( msgboxOutput );

    Thanks.

  • Gillissie

    My intent was to get you to realize you're looking for the equivalent of sprintf. I was hoping you would read the online help for sprintf, where you would have discovered that the .Net equivalent of sprintf was:



    System::String::Format< XML:NAMESPACE PREFIX = MSHelp NS = "http://msdn.microsoft.com/mshelp" />

     



    Then I was hoping you would click on the link to find information on that function. You would click on the first overload, and discover the ins and outs of String::Format(String^, Object^), and learnt that .Net carried slightly different syntax to sprintf. Then after having read the documentation, you would have realized you could have done:


    String ^msgBoxInput = String::Format("You entered {0}", textBox1->Text);

     

    And then you would have realised how useful Online Help could be. I'm a little disappointed that didn't happen.

    At least you know about String::Format now, so when the time comes when you need to build something complex like:


    ("Are you sure you want to {0} the {1} named, {2} to the {3} \"{4}\"", EnumMoveOrCopy.ToString(), "system file", fileName, "folder", "C:\Recycled");

     

    You'll know how to do it.

    ps. don't forget to mark a post as answered.

  • JessicaM

    Hello kvwood,

    If you are looking for the .Net equivalents of C functions, you should consult the Visual Studio help for them. At the bottom of the printf help page, you can clearly see the .Net Framework Equivalents to printf is are:

    Collapse image.NET Framework Equivalent

    • < XML:NAMESPACE PREFIX = MSHelp NS = "http://msdn.microsoft.com/mshelp" />System::Console::Write

    • System::Console::WriteLine

    (it only looks large because I directly pulled the text from Visual studio help, including the HTML formatting).

    you'll find this section in all the help topics on the C library.

    What's that System::Console::WriteLine isn't what you want Could it be that you didn't actually want the equivalent of printf, you really wanted the equivalent of sprintf

    By the way, you could have also built your string dynamically like
    "You entered " + msgBoxInput;



  • VC++ Express Messagebox::Show() Coding Question