+ Reply to Thread
Results 1 to 7 of 7

Thread: MessageBox in a Console Window

  1. #1
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    MessageBox in a Console Window

    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:

    Code:
    using System.Windows.Forms;
    5. Now go to the main function and enter between the two curly braces for the main function:

    Code:
    MessageBox.Show("Hello World");
    6. Now go to Debug -> Start without Debugging.

    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!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: MessageBox in a Console Window

    Thanks for the tutorial. What about adding different options to the MessageBox (Like YesNo)?

    +rep

  4. #3
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    Re: MessageBox in a Console Window

    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!

  5. #4
    Jordan Guest

    Re: MessageBox in a Console Window

    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)?

  6. #5
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    Re: MessageBox in a Console Window

    Do I get a cookie if I explain it right?

    In this bit of code,
    Code:
    msgboxyesorno = MessageBox.Show("Press a button", "", MessageBoxButtons.YesNo);
    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.

    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!

  7. #6
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    Re: MessageBox in a Console Window

    *hands over a cookie*

  8. #7
    bishisht is offline Newbie
    Join Date
    Apr 2010
    Location
    Kathmandu,Nepal
    Posts
    23
    Rep Power
    0

    Re: MessageBox in a Console Window

    great tip

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. A Win32 MessageBox Unicode question
    By Smilex in forum C and C++
    Replies: 2
    Last Post: 03-19-2011, 03:54 PM
  2. How do I display timespan in MessageBox
    By marioDunn in forum C# Programming
    Replies: 2
    Last Post: 05-04-2010, 10:14 PM
  3. Console window problem
    By X_Programmer in forum C# Programming
    Replies: 3
    Last Post: 12-15-2009, 05:20 AM
  4. Messagebox
    By smash_point in forum C and C++
    Replies: 7
    Last Post: 08-11-2008, 11:38 AM
  5. MessageBox
    By Chan in forum C# Programming
    Replies: 1
    Last Post: 12-03-2006, 12:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

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