23

I have just installed C# for the first time, and at first glance it appears to be very similar to VB6. I decided to start off by trying to make a 'Hello, World!' UI Edition.

I started in the Form Designer and made a button named "Click Me!" proceeded to double-click it and typed in

MessageBox("Hello, World!");

I received the following error:

MessageBox is a 'type' but used as a 'variable'

Fair enough, it seems in C# MessageBox is an Object. I tried the following

MessageBox a = new MessageBox("Hello, World!");

I received the following error: MessageBox does not contain a constructor that takes '1' arguments

Now I am stumped. Please help.

Nick Stinemates
  • 41,511
  • 21
  • 59
  • 60
  • 2
    As you learn C#, keep its object-oriented foundation in mind. Unlike js or php, where your method calls are often `method()`, in C# your calls are `object.method()`. Eg: `Console.WriteLine()` as opposed to `print()`. – Dinah Sep 23 '13 at 03:45

8 Answers8

49

MessageBox.Show also returns a DialogResult, which if you put some buttons on there, means you can have it returned what the user clicked. Most of the time I write something like

if (MessageBox.Show("Do you want to continue?", "Question", MessageBoxButtons.YesNo) == MessageBoxResult.Yes) {
     //some interesting behaviour here
}

which I guess is a bit unwieldy but it gets the job done.

See https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.dialogresult for additional enum options you can use here.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Merus
  • 8,796
  • 5
  • 28
  • 41
26

Code summary:

using System.Windows.Forms;

...

MessageBox.Show( "hello world" );

Also (as per this other stack post): In Visual Studio expand the project in Solution Tree, right click on References, Add Reference, Select System.Windows.Forms on Framework tab. This will get the MessageBox working in conjunction with the using System.Windows.Forms reference from above.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
moobaa
  • 4,482
  • 1
  • 29
  • 31
9

It is a static function on the MessageBox class, the simple way to do this is using

MessageBox.Show("my message");

in the System.Windows.Forms class. You can find more on the msdn page for this here . Among other things you can control the message box text, title, default button, and icons. Since you didn't specify, if you are trying to do this in a webpage you should look at triggering the javascript alert("my message"); or confirm("my question"); functions.

George Mauer
  • 117,483
  • 131
  • 382
  • 612
6

Try below code:

MessageBox.Show("Test Information Message", "Caption", MessageBoxButtons.OK, MessageBoxIcon.Information);
AminM
  • 1,658
  • 4
  • 32
  • 48
Sagar Dev Timilsina
  • 1,310
  • 15
  • 32
1

Also you can use a MessageBox with OKCancel options, but it requires many codes. The if block is for OK, the else block is for Cancel. Here is the code:

if (MessageBox.Show("Are you sure you want to do this?", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
    MessageBox.Show("You pressed OK!");
}
else
{
    MessageBox.Show("You pressed Cancel!");
}

You can also use a MessageBox with YesNo options:

if (MessageBox.Show("Are you sure want to doing this?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
    MessageBox.Show("You are pressed Yes!");
}
else
{
    MessageBox.Show("You are pressed No!");
}
rob006
  • 21,383
  • 5
  • 53
  • 74
MicroDOS
  • 1
  • 6
0

In the System.Windows.Forms class, you can find more on the MSDN page for this here. Among other things you can control the message box text, title, default button, and icons. Since you didn't specify, if you are trying to do this in a webpage you should look at triggering the javascript alert("my message"); or confirm("my question"); functions.

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
0

This is some of the things you can put into a message box. Enjoy
MessageBox.Show("Enter the text for the message box",
"Enter the name of the message box",
(Enter the button names e.g. MessageBoxButtons.YesNo),
(Enter the icon e.g. MessageBoxIcon.Question),
(Enter the default button e.g. MessageBoxDefaultButton.Button1)

More information can be found here

user2680314
  • 1
  • 1
  • 2
0

I got the same error 'System.Windows.Forms.MessageBox' is a 'type' but is used like a 'variable', even if using:

MessageBox.Show("Hello, World!");

I guess my initial attempts with invalid syntax caused some kind of bug and I ended up fixing it by adding a space between "MessageBox.Show" and the brackets ():

MessageBox.Show ("Hello, World!");

Now using the original syntax without the extra space works again:

MessageBox.Show("Hello, World!");
KeithJ
  • 169
  • 3
  • 12
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](http://stackoverflow.com/questions/ask). You can also [add a bounty](http://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question once you have enough [reputation](http://stackoverflow.com/help/whats-reputation). – Anik Islam Abhi Mar 09 '15 at 03:00
  • This answers the question indirectly, because this was the solution that resolved my issue, since I was getting the same error as the original poster. My question was also how to create a messagebox in C#, but none of the answers here resolved my issue. – KeithJ Mar 09 '15 at 03:29