What we want to create is a simple calculator application. What it will do is to add two numbers inserted
by the user. To start, we will need three text‐boxes: Two for the two numbers that the user wants to add
and the third for the result. We will also need a button so that the user can press it and receive he’s result.
To do all this, click on the “Text Box” control in the toolbox, and then click on your form. As you can see, a
text box appeared on your form. Repeat this step again and create two more text boxes. Align the text
boxes the same way I did:
Now, select the button control from the toolbox and create a button on the form.
Good, we now created all the controls we need for our application. But, there is a problem, why is the
button named “Button1”? Because this is how it is by default, to change that, we need to change its
properties. By default, the properties window is not opened in Visual C#. To open it, go to “View” and click
on “Properties”.
The properties panel (obviously) shows the select controls properties, such as height, width, color, text,
etc… In this case, we only need to change the text since the button can be resized with using the mouse.
Click on the button (Make sure you don’t double click it, or its code will open. If that happens, close the tab
with the code from the top of the middle‐panel). Once clicked, the button’s properties will appear in the
“Properties” window. Scroll down and go to “Text”. To its right you will see “Button1”. Change that to
“Add”, and press enter.
Your button now has “Add” written on it. Very good, this way you can edit every item’s properties, even
the text boxes’.
Also, you might notice that the form’s name is “Form1”. Try to do something about it.
How To: Click on an empty space on the form, change the form’s text property to “Calculator”
Debugging the application
Of course, we added the controls on our form, but the button doesn’t do anything since we didn’t “tell” it
do to anything. You can see your program running by pressing the “Debug” button, the green arrow in the
toolbar ( ). When you click the debug button, you application will start. It should look something like
this:
You probably tried to click the button already, and noticed how well he is doing his job. Debugging is the
best method to test your application before publishing it. Every time you make a change to it, you debug it
to see if it worked. Once you finish the application, you build the entire project turning everything into one
executable, and probably make a setup for your application.
Coding the application
To make the application work, you obviously have to write some code. If you are still debugging your
application, close it and go back to your project. Now, double‐click on your button to open the code
window. As you can see, everything is tabbed in the project. You can always go back to the form designer
by clicking its tab on the top.
With all the amount of code that you have in front of you, you might get scared (again!). Don’t worry you
will get used to it, and to even more than that. If you are reading this tutorial, let’s hope you already know
the basics of another programming language, if not it will be hard for you to search Wikipedia for every
word you don’t understand, but it would be useful.
The first statements in the code import some items from the .NET Framework. What is that you might ask,
the .NET Framework is a large library of coded solutions to common programming problems that
manages the execution of programs written specifically for the framework. To be clearer, it is a large
amount of code already written for you, so you can build programs with a pretty user interface and other
things. It helps you very much because it reduces the amount of things that you need to learn to create the
entire user interface. We should all thank Microsoft for it:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
By “import some things” from the .NET Framework, I meant importing some classes. Those classes can be
the forms, the user controls, and all the other things that helped us by now creating the program. You will
learn the meaning of them later.
For now, let’s see the rest of the code:
namespace Calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
The “public Form1()” statement is a class that is executed when we start the program; actually, when we
open the form named “Form1.cs” (“.cs” is from C Sharp). In case you did not know, in C# the code is
usually put between curly braces just like in Java and C++.
The “private void button1_Click(object sender, EventArgs e)” is that class that is executed when
we click the button. Inside it, we will write the code that will add the two values from the text boxes.
Note: In C#, two slashes (//) represents the beginning of a comment. A comment is a piece of code that is not
executed by the compiler, it is used to help you organize you code, and so that other programmers will
understand what every piece of code means. We will use comments inside our codes for better explanation.
To make that button add the two values and return the sum we need, we have to grab the text content
from the two text boxes, turn it to integers, add them, and change the text of the third text box to the sum.
It is very simple:
double val1, val2; //We declare two double type variables
//We assign to the first variable the value of the text box
//Since the text box cand hold a string, it must be converted
//to a double to assign it to "val1".
//Note that we assign using “=” as an operator
val1 = Double.Parse(textBox1.Text);
//Double.Parse("string") converts the string put into the brackets
//and assigns it to a double
//Same thing for the second variable
val2 = Double.Parse(textBox2.Text);
//Now we are doing the exact oposite, we take the two
//double values and we convert their sum to a string
//using the .ToString() command
textBox3.Text = (val1 + val2).ToString();
Now that we finished you might want to debug your project and see if it works.
What we did is easy to explain; we declared two variables and assigned the values of the two text boxes
after we converted them from strings to integers. Then, we changed the text of the third text box into the
sum of the two.
Publishing you application
What you have to do is to create an icon for your application, change its publishing settings and make a
setup for it, but we will skip these steps as they are not related to basic C# programming.
Note: Check out the “Advanced Visual C# Programming” tutorial once you finish this one.
"News powered by"
No comments:
Post a Comment