Home

The Forms of an Application: Introduction

 

Introduction

The form is the most fundamental object used in an application. By itself, a form does nothing. Its main role is to host other objects that the user uses to interact with the computer:

Form 1
 

Form Creation

There are various ways you can get a form to your application:

  • If you create a Windows Forms Application, it creates a starting form for you
  • After starting an empty project or a Windows Forms Application, you can add a form to it. To do this, on the main menu, you can click Project -> Add New Item... Select Windows Form. Give it a name and click OK
  • You can dynamically create a form and add it to your application.

In Lesson 2, we saw that a was based on the Form class that is defined in the System.Windows.Forms namespace created in the System.Windows.Forms.dll assembly. Therefore, if you start an application from scratch and you want to use a form in it, you can include the System.Windows.Forms.dll library to your application. To refer to a form, you can include the System.Windows.Forms namespace in your application.

As seen in Lesson 2, to create a form-based application, you can derive a class from Form. Here is an example:

public class Exercise : System.Windows.Forms.Form
{
    private void InitializeComponent()
    {
    }

    public Exercise()
    {
        InitializeComponent();
    }
}

public class Program
{
    static int Main()
    {
        System.Windows.Forms.Application.Run(new Exercise());
        return 0;
    }
}

Practical LearningPractical Learning: Introducing Forms

  1. Start Microsoft Visual C# and create a new Windows Application named CPAS1
  2. Press Ctrl + F5 to test the program
  3. Close the form and return to your programming environment

The Name of a Form

Like any other control, a form must have a name. If you derive a class from Form, the name you use for the class would be the name of the form. If you add a form from the Add New Item dialog box, you must also give it a name. If you create a Windows Application from the New Project dialog box, the wizard would create a default form for you named Form1. However you create or get a form, you can change its name.

To change the name of a form, in the Solution Explorer, right-click the name of the form and type a new name with the .cs extension.

Practical LearningPractical Learning: Renaming a Form

  1. In the Solution Explorer, right-click Form1.cs and click Rename
  2. Type Central.cs and press Enter
 

Home Copyright © 2007 FunctionX, Inc. Next