This tutorial will show you how to show up a MessageBox in a console application. I am using Visual C# 2008 Express Edition for the tutorial.
1. Start a new project - File -> New Project -> Console Application -> OK.
2. On the right side of the screen (unless you've changed its position) is the Solution Explorer - right click on References and click Add Reference. On my machine this takes a little while to load, so don't get impatient with it.
3. Scroll down the list until you find "System.Windows.Forms", click it and then click OK
4. Now under all the using _____ 's add this to the code window:
5. Now go to the main function and enter between the two curly braces for the main function:Code:using System.Windows.Forms;
6. Now go to Debug -> Start without Debugging.Code:MessageBox.Show("Hello World");
VOILA! You have now made a messagebox show in a console application![]()
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
Thanks for the tutorial. What about adding different options to the MessageBox (Like YesNo)?
+rep
Then you could do something like this:
Code:DialogResult msgboxyesorno; msgboxyesorno = MessageBox.Show("Press a button", "", MessageBoxButtons.YesNo); if (msgboxyesorno == DialogResult.Yes) { Console.WriteLine("You pressed yes!"); } else { Console.WriteLine("You pressed no!"); }
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
If I didn't know what you changed there (if I were a noob) I'd have no idea what you did to add yes/no. Perhaps some elaboration on that second bit of code about the third bit of code you passed and why you assign the result to variable (which you then test)?
Do I get a cookie if I explain it right?
In this bit of code,
I have had to add a new parameter, for the Message Box's caption, as C# does not have optional parameters. I have also added a third parameter which allows me to use Message Box buttons, these could be a number of other things but since we only want Yes and No, we use MessageBoxButtons.YesNo. We have to assign it to a variable, so that we can get the result, Yes or No. As everything is an object in C#, if you press yes, it assigns the object DialogResult.Yes to the variable and if you press No, it assigns the object DialogResult.No to the variable. By then testing the variable against these two objects (DialogResult.No and DialogResult.Yes), we can determine which button was pressed.Code:msgboxyesorno = MessageBox.Show("Press a button", "", MessageBoxButtons.YesNo);
Cookie now Jordan?
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
*hands over a cookie*![]()
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
great tip
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks