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:
Now type "ess" after the "M". IntelliSense will jump down:
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:
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:
Now type a full stop (period) after the "x" of MessageBox. The IntelliSense list will appear again:
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:
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":
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 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:
Click your button to see your Message Box:
Congratulations! It's your first message! In the next part, we'll explore other things you can do with a message box.
Comments
Post a Comment