C # Message Box, variable use

I searched, but I don’t know if I use the correct wording for the search. I am writing a C # program for my class, but I am having problems with the message field.

I am trying to have a message box display a message and at the same time read a variable. I have no problem with this in console applications, but I can't figure it out for Windows.

So far I:

MessageBox.Show("You are right, it only took you {0} guesses!!!", "Results", MessageBoxButtons.OK);

Which works great. Howerver I'm trying to {0} be the result of the variable numGuesses. I'm sure it is simple, and I just forget about it in the book or something like that, or my syntax is incorrect somewhere.

+5
source share
5 answers

to try

MessageBox.Show(string.Format ("You are right, it only took you {0} guesses!!!", numGuesses ), "Results", MessageBoxButtons.OK);

. http://msdn.microsoft.com/en-us/library/system.string.format(v=VS.100).aspx

+11

String.Format .

MessageBox.Show(String.Format("You are right, it only took you {0} guesses!!!", myVariable), "Results", MessageBoxButtons.OK);

http://msdn.microsoft.com/en-us/library/system.string.format(v=VS.100).aspx

:

MessageBox.Show("You are right, it only took you " + myVariable + " guesses!!!", "Results", MessageBoxButtons.OK);

, String.Format, .

+3

What about String.Format () ?

MessageBox.Show(String.Format("You are right, it only took you {0} guesses!!!", numGuesses), "Results", MessageBoxButtons.OK);
+1
source

String.Format is what you want:

string message = string.Format("You are right, it only took you {0} guesses!!!",numGuesses)

MessageBox.Show(message, "Results", MessageBoxButtons.OK);
+1
source
MessageBox.Show(
                  string.Format(
                                 "You are right, it only took you {0} guesses!!!",
                                Results
                               ), 
                  MessageBoxButtons.OK
               );
+1
source

All Articles