C# Windows Forms

C# programmers have made extensive use of forms to build user interfaces. Each time you create a Windows application, Visual Studio will display a default blank form, onto which you can drag the controls onto your applications main form and adjust their size and position.

The first job is to start a new project and build a form. Open your Visual Studio and select File->New Project and from the new project dialog box select Other Languages->Visual C# and select Windows Forms Application. Enter a project name and click OK button. The following picture shows how to create a new Form in Visual Studio.

New Project

Select Windows Forms Application from New Project dialog box.

Windows Application

After selecting Windows Forms Application , you can see a default Form (Form1) in your new C# project. The Windows Form you see in Designer view is a visual representation of the window that will open when your application is opened. You can switch between this view and Code view at any time by right-clicking the design surface or code window and then clicking View Code or View Designer. The following picture shows how is the default Form (Form1) looks like.

C# Form

At the top of the form there is a title bar which displays the forms title. Form1 is the default name, and you can change the name to your convenience . The title bar also includes the control box, which holds the minimize, maximize, and close buttons.

If you want to set any properties of the Form, you can use Visual Studio Property window to change it. If you do not see the Properties window, on the View menu, click Properties window. This window lists the properties of the currently selected Windows Form or control, and its here that you can change the existing values.

C# Properties

For example , to change the forms title from Form1 to MyForm, click on Form1 and move to the right side down Properties window, set Text property to MyForm. Then you can see the Title of the form is changed. Likewise you can set any properties of Form through Properties window.

You can also set the properties of the Form1 through coding.

For example , if you want to change the back color of the form to Brown , you can code like this.

  this.BackColor = Color.Brown;

Likewise you can change other properties of Form1 through coding.

The following C# source code shows how to change the Title, BackColor, Size, Location and MaximizeBox properties of Form1. Copy and paste the following C# source code to source code editor of your Visual Studio.




using System;
using System.Drawing;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Change Prperties Through Coding";
            this.BackColor = Color.Brown;
            this.Size = new Size(350, 125);
            this.Location = new Point(300, 300);
            this.MaximizeBox = false;
        }
    }
}

When you execute (press F5 key) the program the form is look like the following image.
Program Output


The Windows based programs you create using C# run in the context of a form. When you close the form, the application also ends.


 C# GUI - Related Contents



.