Skip to main content

...

....

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

Popular posts from this blog

C# Snippet - Shuffling a Dictionary [Beginner]

Randomizing something can be a daunting task, especially with all the algorithms out there. However, sometimes you just need to shuffle things up, in a simple, yet effective manner. Today we are going to take a quick look at an easy and simple way to randomize a dictionary, which is most likely something that you may be using in a complex application. The tricky thing about ordering dictionaries is that...well they are not ordered to begin with. Typically they are a chaotic collection of key/value pairs. There is no first element or last element, just elements. This is why it is a little tricky to randomize them. Before we get started, we need to build a quick dictionary. For this tutorial, we will be doing an extremely simple string/int dictionary, but rest assured the steps we take can be used for any kind of dictionary you can come up with, no matter what object types you use. Dictionary < String , int > origin = new Dictionary < string , int >();

C# WPF Printing Part 2 - Pagination [Intermediate]

About two weeks ago, we had a tutorial here at SOTC on the basics of printing in WPF . It covered the standard stuff, like popping the print dialog, and what you needed to do to print visuals (both created in XAML and on the fly). But really, that's barely scratching the surface - any decent printing system in pretty much any application needs to be able to do a lot more than that. So today, we are going to take one more baby step forward into the world of printing - we are going to take a look at pagination. The main class that we will need to do pagination is the DocumentPaginator . I mentioned this class very briefly in the previous tutorial, but only in the context of the printing methods on PrintDialog , PrintVisual (which we focused on last time) and PrintDocument (which we will be focusing on today). This PrintDocument function takes a DocumentPaginator to print - and this is why we need to create one. Unfortunately, making a DocumentPaginator is not as easy as

C# WPF Tutorial - Implementing IScrollInfo [Advanced]

The ScrollViewer in WPF is pretty handy (and quite flexible) - especially when compared to what you had to work with in WinForms ( ScrollableControl ). 98% of the time, I can make the ScrollViewer do what I need it to for the given situation. Those other 2 percent, though, can get kind of hairy. Fortunately, WPF provides the IScrollInfo interface - which is what we will be talking about today. So what is IScrollInfo ? Well, it is a way to take over the logic behind scrolling, while still maintaining the look and feel of the standard ScrollViewer . Now, first off, why in the world would we want to do that? To answer that question, I'm going to take a an example from a tutorial that is over a year old now - Creating a Custom Panel Control . In that tutorial, we created our own custom WPF panel (that animated!). One of the issues with that panel though (and the WPF WrapPanel in general) is that you have to disable the horizontal scrollbar if you put the panel in a ScrollV