Blogger templates

Pages

Tuesday 22 October 2013

A C# MessageBox

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 block 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":
Type a left round bracket
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 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.

If you look at the message box we created in the previous section, you'll notice there's no Title in the blue area to the left of the red X - it's blank:
Click the button on your C# Form
You can add a Title quite easily.
Click OK on your Message Box. Then click the Red X on your programme to exit it. This will return you to Visual C#. Go back to the coding window (press F7 on your keyboard, if you can't see it).
Position your cursor after the final double quote of "My First Message", circled in red in the image below:
Position your cursor
Now type a comma. As soon as you type a comma, you'll see the list of Show options again:
The options for the C# Show Method
Type the following:
"Message"
Again, you need the double quotes. But your line of code should look like this:

A Title has been added
When your line of code looks like the one above, Run your programme again. Click your button and you should see a Title on your Message Box:
The title is now displayed on the messagebox

Other Button Options

Rather than having just an OK button, you can add buttons like Yes, No, and Cancel to your C# message boxes. We'll add a Yes and a No button.
Return to your coding window. After the second double quote of the Title you've just added, type another comma. Hit the spacebar on your keyboard once, and you'll see the IntelliSense list appear. (If it doesn't appear, just type a capital letter "M").

Message Box Buttons in C#
The one that adds buttons to a message box is, you won't be surprised to hear, MessageBoxButtons. Press the enter key on your keyboard when this option is highlighted. It will be added to the your code. Now type a full stop (period) after the final "s" of MessageBoxButtons. You'll see the button options:
The button options
Double click the one for YesNo, and it will be added to your code.
Run your programme again, and click your button. Your Message Box will then look like this:
What the Yes and No buttons look like

Adding Icons to a C# Message Box

Another thing you can add to brighten up your Message Box is an Icon. It's easier to see what these are than to explain!
Type another comma after MessageBoxButtons.YesNo. After the comma, type a capital letter "M" again. From the IntelliSense list that appears, double click MessageBoxIcon. After MessageBoxIcon, type a full stop to see the available icons:
MessageBox Icons
We've gone for Asterisk. Double click this to add it to your code. Run your programme again to see what the icon looks like on your Message Box:
An Asterisk has been added to the Message Box
Looks pretty impressive, hey! And all that with one line of code!

0 comments:

Post a Comment