A C# MessageBox

<< Continues from the previous lesson

We want to display a message box, with some text on it. This is quite easy to do in C#.

Position your cursor between the two curly brackets. Then type a capital letter "M". You'll see the IntelliSense list appear:

The IntelliSense List in C#

Now type "ess" after the "M". IntelliSense will jump down:

Message is now highlighted

The only options that start with Mess are all Message ones. The one we want is MessageBox. You can either just type the rest, or even easier is to press the down arrow on your keyboard to move down to MessageBox:

MessageBox is now selected

When you have MessageBox selected, hit the enter key on your keyboard (or double click the entry on the list). The code will be added for you:

The code is added for you

Now type a full stop (period) after the "x" of MessageBox. The IntelliSense list will appear again:

There are three Methods for MessageBox

There are only three items on the list now, and all Methods (you can tell they are Methods because they have the purple cube icon next to them) Double click on Show, and it will be added to your C# code:

Selected the Show Method

Because Show is a Method, we need some round brackets. The text for our message box will go between the round brackets. So type a left round bracket, just after the "w" of "Show":

Number of results for the Show method in C#

As soon as you type the left round bracket after the "w", you'll see all the different ways that the Show method can be used. There are 21 different ways in total. Fortunately, you don't have to hunt through them all! Type the following, after the left round bracket (Don't forget the double quotation marks.):

"My First Message"

After the final double quote mark, type a right round bracket. Then finish off the line by typing a semi-colon ( ; ), Your coding window will then look like this:

The full C# Message

The text in the dark reddish colour is what will be displayed in your message box. To try it out, save your work by clicking File from the menu bar at the top of Visual Studio. From the File menu, click Save All. You'll then see the same Save box you saw for the Console Application. Save the project.

Run your programme by clicking Debug > Start Debugging (or Start Without Debugging). Or just press the F5 key on your keyboard. Your programme will look like this:

The form with a button

Click your button to see your Message Box:

Click the button on your C# Form

Congratulations! It's your first message! In the next part, we'll explore other things you can do with a message box.