Welcome to Wynthar's C# Tutorial Series Tutorial 2: More Windows Forms Features Introduction
So in the
Last Tutorial we downloaded Microsoft Visual C# Express Edition and created our first windows forms application. I hope you have at least tried to play around with the code we used. Experiment, change the string. See the results. If you have not managed to make enough changes that the program does not compile, you are not experimenting enough.
Let's Begin
When creating forms, I usually find that it's easiest to develop the user interface the way I want it to work first, then program in the functionality for all of the forms objects. So, let's add a pair of buttons, a check box, a text box and see what we can do with these. So, let's create our Tutorial 2 project.
Go to File->New Project... Select Windows Forms Application. Set the Name to Tutorial2 and click OK.
Next
In the tool box, click on the [+] Common Controls to expand that menu. And click on Button.
Now go over to your form, and click in the middle of your form. It should create a button for you.
Repeat this process once more to create two buttons.
Next
Now, scroll down in the tool box and select TextBox. Click somewhere to the right of your first button.
Next
I had to scroll up a little to find CheckBox. Click on CheckBox and click on our form to the right of the second button.
Next
Let's add one more item to our form. Click on Label and add a label someplace below the other objects we already have.
Next
Excellent. Our user interface for this tutorial is now complete.
Now, let's add some functionality to the program.
First, let's add some functionality to button1. So, double click on button1.
Enter the following into the function where the cursor is.
Code:
private void button1_Click(object sender, EventArgs e)
{
//This takes the value out of the textBox1
//And changes the form's title to the value
//Of the text box.
string newWindowTitle = "Title: " + textBox1.Text;
this.Text = newWindowTitle;
}
Code Explaination
So. I'll explain this code a bit. The forward slashes are comments. Comments are not read by the compiler. They are useful for making your code easier to read. It can also be helpful to add in reminders to yourself about what is happening in your code.
I created a new variable of type "string" with a variable name of "newWindowTitle" and I set it equal to the string "Title: " then I attached the text from textBox1 to the end of the string.
For example, if someone types "Hello" into the text box, then our string newWindowTitle will equal "Title: " + "Hello", which is added together and results in a single string, "Title: Hello"
The final line of our function sets the current form's title to our string variable newWindowTitle. The keyword "this" refers to whichever object we are currently in. Since, our code currently resides in Form1, "this" refers to Form1.
I have found out that the Form1.Text refers to the form's title. Therefore, when we set this.Text = newWindowTitle; we will be changing the form's title to "Title: Whatever Is In The Text Box"
Next
Okay, so now go back to the tab Form1.
cs [Design] then double click on button2 to create a method stub for button2. Enter the following code.
Code:
private void button2_Click(object sender, EventArgs e)
{
//This button changes the text on the label
//based on check box value.
if (checkBox1.Checked == true)
{
label1.Text = "Check box is checked!";
}
else
{
label1.Text = "You didn't check the checkbox.";
}
}
Code Explaination
Okay. Another fun feature of programming. The If/Else statement. It should be pretty logical. We check here if our checkBox1 is checked by accessing the checkBox1.Checked variable and seeing if it equals (==) true.
If it does equal true, that means it is checked. Then we go execute the code between the braces. This code for the true case reads label1.Text = "Check box is checked!" Similar to setting the text of the form title, this will set the text for the label to our string.
Next
So, now press the green arrow again to compile and run. (Or hotkey F5)
Finally
If everything is correct you should be able to test out your form and the buttons. Here is a sample result that I came up with by playing with the form.
[Next Time]- We will go over Form Object Properties and how to set them.
- Once again we will explore and practice more coding examples.
[Later]- If I get good feedback, I will continue these tutorials all the way up to making a simple map hack using some common offsets.
- Please feel free to post your feedback, comments, questions, and other things you might like to see.
- Thanks for reading guys!
[C# Tutorials]