What we want to do now is to display a message box whenever the button is clicked. So we need the coding window. To see the code for the button, double click the button you added to the Form. When you do, the coding window will open, and your cursor will be flashing inside of the button code. It will look like this:
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:
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.):
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, clickSave 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.
The only difference from the last time you saw this screen is the addition of the code for the button. This code:
private void button1_Click(object sender, EventArgs e)
{
{
}
This is just another Method, a piece of code that does something. The name of the Method isbutton1_Click. It's called button1 because that's currently the Name of the button. When you changed the Text, Location, and Size properties of the button, you could have also changed the Name property from button1 (the default Name) to something else.
The _Click part after button1 is called an Event. Other events are MouseDown, LocationChanged, TextChanged, and lots more. You'll learn more about Events later.
After _Click, and in between a pair of round brackets, we have this:
object sender, EventArgs e
These two are know as arguments. One arguments is called sender, and the other is called e. Again, you'll learn more about arguments later, so don't worry about them for now.
Notice that there is a pair of curly brackets for the button code:
private void button1_Click(object sender, EventArgs e)
{
{
}
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":
"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:Run your programme by clicking Debug > Start Debugging. Or just press the F5 key on your keyboard. Your programme will look like this:
No comments:
Post a Comment