A Simple Application in C#



In this tutorial, you’ ll see how to use variables. You’ll see how to set up both text and number variables. We will write a programme that takes text from a text box, store the text in a variable and display in a message box. A variable is just a storage area for holding things that you'll need later. Let`s look this example:

First add a button and a label in your form and at properties window put the name and the text for the button, while for the label put only the text, and in this case the name and text for the button are: 
Form in C#

Name:  btnFullName

 Text: Full Name

Text for a label is:


Text: First Name









The label on your form you can add by dragging the mouse on the right side of the window to display the toolbox by selecting the option "Lable"


Lable in C#


In the same way add a another label in your form with the following text:


Text: Last Name


Simple Application


Afterwards move your mouse again to the left side of the screen at the Toolbox window and choose the TextBox option.


TextBox in C#

Click on your form. And new TextBox is added to your form. Put the name of this textbox.


 Name: txtFirstName

Add TextBox in Your Form

In the same way add a another textbox and put its name:


Name: txtLastName


C# Simple Application

Double click on the button and C# will generate the code for your button.


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;

namespace FullName
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnFullName_Click(object sender, EventArgs e)
        {

        }
    }

}

























To put a string variable inside the brackets where is generated the code of the button, we have to assign two things: Type of variable you want, and a name for your variable.

Click inside the brackets where is put the code for the button and put this:

string FirstName;


namespace FullName
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnFullName_Click(object sender, EventArgs e)
        {
            string FirstName;

        }
    }
}






















The variable type is string, while its name is FirstName. Other variable types are int, float, double, decimal. Examples of different variables you can find if you click on the right side of the blog in C# Language and click there the Variables option.

The only characters that you can use in your variable names are letters, numbers, and the underscore character ( _ ). And you must start the variable name with a letter, or underscore. You'll get an error message if you start your variable names with a number. So these are OK:

Example:

FirstName
Last_Name
FirstName1
LastName_1

After you have assigned the type and name of variable, the next step you have to do is to save something at the variable and this you can do as follows:

FirstName = txtFirstName.Text;
LastName = txtLastName.Text;

The code in your window will look like this:


namespace FullName
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnFullName_Click(object sender, EventArgs e)
        {
            string FirstName;
            string LastName;

            FirstName = txtFirstName.Text;
            LastName = txtLastName.Text;

        }
    }
}






















And now add a messagebox:
        private void btnFullName_Click(object sender, EventArgs e)
        {
            string FirstName;
            string LastName;

            FirstName = txtFirstName.Text;
            LastName = txtLastName.Text;

            MessageBox.Show(" Your FullName is : " + FirstName + " " + LastName);

        }


Afterwards execute your programme clicking DebugàStartDebugging or press F5 key on your keyboard.


C# Simple Application


Finally click on the Full Name button and there will come out the full name:


Execute a Simple Application


No comments:

Post a Comment