Home

The Forms of an Application

 

Fundamentals of a Form

 

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:

#include <windows.h>

#using <System.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Windows::Forms;

public ref class CExercise : public Form
{
public:
    CExercise()
    {
	InitializeComponent();
    }

    void InitializeComponent()
    {
    }
};

int APIENTRY WinMain(HINSTANCE hInstance,
		     HINSTANCE hPrevInstance,
		     LPSTR lpCmdLine,
		     int nCmdShow)
{
    Application::Run(gcnew CExercise());

    return 0;
}

Practical LearningPractical Learning: Introducing Forms

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

Home Copyright © 2007 FunctionX, Inc. Next