Your name in a MessageBox
So, you've learned how to create a simple Form, and now it's time to learn what can you do with it. Of course, we'll start with something very, very simple. In this tiny tutorial, you will learn how to add items on the Form, change some of their properties and make them work.
Step 1:
You see the Toolbox on the left? Cool, I need you to drag and drop the following items on the Form: Label, TextBox and Button. You can change their name in the Properties Windows on the right. Scroll down and look for the Text property. That will change the default name of the item. I changed the Form's name here to MessageBox Name. Call it whatever you want. Change the Label name too, and of course, the Button name.
Step 2:
So, now what? The TextBox reads characters from the keyboard, and shows them in a MessageBox when you click the Button. In this example, the Label is just a simple text on the Form, that doesn't do anything when clicked. It's just a label, labeling something. But, how do you make it work, anyway? You add code. To add code to a specific item and make it do something, you click on it. Now what do we need here, again? We need the Button, when clicked, to perform an action, which would be reading the content entered in the TextBox, and shown as a MessageBox.
This code here will do that. Click the Button, and add these lines:
private void button1_Click(object sender, EventArgs e)Visual Studio generates code, you will write the line that is between the curly brackets. In the MessageBox, it will show the text "Your name is " plus the name that you type in the TextBox, as one sentence. Now, after you wrote your first line of code, press F5 to see how the application works. The result should look like this:
{
MessageBox.Show ("Your name is " + textBox1.Text);
}