C# Dialogs, Part 1 - MessageBox [Beginner]


We have all seen them, and most of the time they mark something bad. They are dialog boxes and not only have they been the bane of all computer users since the GUI was invented, but they are really a key to any software project. Whether you are making a simple text editor, or you programming a time viewing machine, chances are you at some point going to need a dialog box. Luckily, with C# adding one is super easy.

To start off we will make a simple popup that will alert the user of some useful information. To do this all we need to do is use MessageBox.Show() and that will make a super simple popup window that will tell our user whatever we want. So it would look something like this:
MessageBox.Show("SOTC has the best tutorials!")
 
All this will do is bring up a small dialog box, with the message being whatever the text is. Nothing special, but it can get the job done if you need something simple. All the user has to do is either hit OK or the X button and the message will disappear. But how do we capture what the user chooses? It is extremely simple, you just capture the response in an if statement like so:
if (MessageBox.Show("SOTC has the best tutorials!") == DialogResult.OK)
{
  MessageBox.Show("You hit ok.");
}
 
The first thing you will notice is that we use an enum called DialogResult. This is built into the C# libraries and, as you may have guessed, is used in catching dialog results. At this point hitting OK or the X button will both send an DialogResult.OK back, so this will capture either. What do we do if we want more that just one choice? Well, MessageBox can handle many things, and one of those is having more than one button in the dialog. Take at look at this:
if (MessageBox.Show("SOTC has the best tutorials?",
       "SOTC Tutorials", MessageBoxButtons.YesNo)
    == DialogResult.Yes)
{
  MessageBox.Show("Yeah, we know.");
}
else
{
  MessageBox.Show("Well that's just not true.");
}
 
 
In order to choose our buttons we must provide another argument before that, a messagebox "caption", which is the title of the dialog. As you can tell, there is a built in enum for MessageBox buttons as well, which can be lots of things. YesNo, OkCancel, and the like are represented as a simple way to get a quick dialog box up on the screen.

The last thing we will add to our simple dialog is really just cosmetic. You can actually tell your messagebox what standard icon to use, which can be anything from the ever so evil red and white "X" to a windows information icon. If you didn't already guess, there is also an enum for this purpose as well. When it is all finished, our code will look like so:
if (MessageBox.Show("SOTC has the best tutorials?",
       "SOTC Tutorials", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
    == DialogResult.Yes)
{
  MessageBox.Show("Yeah, we know.");
}
else
{
  MessageBox.Show("Well thats just not true.");
}
 
And in case you were worrying about when you can call MessageBox.Show, you can relax. You can call it at pretty much anytime you want (although, for instance, putting a dialog box in a process that runs as a service might not be the brightest of ideas).

Now be careful - don't overuse dialog boxes. Users hate nothing more than a popup box that interrupts their flow and makes them change focus. But there are situations that warrant them, and .NET makes it absurdly easy. In this tutorial we examined the built in dialog options C# has, and in Part 2 we will look over custom dialogs.

Comments