Home  |  Delphi .net Home  |  Windows Forms Applications
 .NET Framework
 Namespace References

 System.Drawing - download only

 Articles and Tutorials

 Overview of .NET
 Delphi and .NET
 Winform Applications
 ASP .Net Applications
 ASP Web Services
 Framework Collections
 Framework String Handling
 Framework Files and Folders

 Author links


 Windows Forms Applications
What are Windows Forms Applications?
A Windows Form Application is the .Net equivalent to a Delphi 7 (or earlier) Win32 application. You create one using the File | Windows Forms Application menu option (or by pressing the little yellow page with a plus sign on it).
When building such an application, Delphi does a lot of work for us - the programmer simply uses the mouse to click, drag, size and position graphical parts to build each screen of the application.
Each part (or element) can be passive (displaying text or graphics), or active (responding to a user mouse or keyboard action).
This is best illustrated with a very simple program.

Creating a simple 'Hello World' program
When you first run Delphi and create a Windows Forms Application, it will prepare on screen a new form called WinForm. The editor screen will comprise a number of windows, including the menu bar, a code editor, component selection lists, and this first form (screen) of our program. Do not worry about the editor window at the moment.
The form should look something like this :

We have shown the form reduced in size for convenience here, but you will find it larger on your computer. It is a blank form, onto which we can add various controls and information. The component list window on the bottom right has a list of graphical (and other) items that you can add to the form. Thee first list you will see is called the Windows Forms list, containing buttons and labels and other things you might want to add to your form. Other lists, (expandable and collapsible by hitting the down and up arrow pairs) include other components such as menus, database components and so on.
We will select the simplest item from the Windows Forms collection. Click on the A Label image to select a Label. This A Label will then show as selected:

Having selected a graphical element, we then mark out on the form where we want to place the element. This is done by clicking and dragging. This gives us our first form element:


Changing graphical element properties
Notice that the graphical element contains the text Label1 as well as resize corners. The caption text is called the Text (in earlier Delphi releases it is called Caption), and will appear when we run the application. This Text is called a Property of the button. The label has many other properties such as height and width, but for now, we are only concerned with the text.
Let us blank out the text. We do this in the window called the Object Inspector :


Adding an active screen element
If we now return to the Windows Forms graphical element collection, and select a button, shown as a very small button with ab on it, we can add this to the form as well:


We can change the buton caption as we did with the label :

We now have a label and a button on the form. But the button will do nothing when pressed until we tell Delphi what we want it to do. This is what modern applications are all about - they respond to user events as and when they happen.
So we must set an action, called an Event, for the button. The main event for a button is a Click. This can be activated simply by double clicking the button on the form.
This will automatically add an event called OnClick for the button, and add a related event handler in the program code:

This 'skeleton' code will not do anything as it stands. We must add some code. Code that we add will run when the button is clicked. So let us change the label caption when the button is pressed.
As we type, Delphi helps us with a list of possible options for the item we are working on. In our instance, we are setting a Label caption:

Here you see that Delphi has listed all appropriate actions that start with Te. If we press Enter, Delphi will complete the currently selected item in the list. We assign a text value 'Hello World' to the caption property. Note that we terminate this line of code with a ; - all Delphi code statements end with this indicator. It allows us to write a command spread across multiple lines - telling Delphi when we have finished the command.

And we have now finished our very simple action - we will set the label to 'Hello World' when the button is pressed.

Running our first program
To run the program, we can click on the Green triangle (like a Video play button), or press F9. When the program runs it looks like this:

When we click on the button, we get:

and our program has set the Label text as we requested.
Note that the program is still running. We can click as many times as we like with the same outcome. Only when we close the program by clicking on the top right X will it terminate.

Looking at the code that Delphi generated
Whilst we have only typed one line of code, Delphi has typed many for us. Let us first look at the main program code. Notice that there are 2 lines marked with a + sign, which are placeholders for required code that you need not change. It is built for you, and you need not concern yourself with it. You can view it by clicking on the + sign if you are particularly interested.
 unit WinForm;
   System.Drawing, System.Collections, System.ComponentModel,
   System.Windows.Forms, System.Data;
   TWinForm = class(System.Windows.Forms.Form)
   +Designer Managed Code
   strict protected
     /// Clean up any resources being used.

     procedure Dispose(Disposing: Boolean); override;
     { private Declarations }
     constructor Create;
   [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]
  +Windows Form Designer generated code
 procedure TWinForm.Dispose(Disposing: Boolean);
   if Disposing then
     if Components <> nil then
   inherited Dispose(Disposing);
 constructor TWinForm.Create;
   inherited Create;
   // Required for Windows Form Designer support
   // TODO: Add any constructor code after InitializeComponent call
 procedure TWinForm.Button1_Click(sender: System.Object; e: System.EventArgs);
   Label1.Text := 'Hello World';

This code is called a Unit and is a Delphi module - one chunk of code. If you save this code, it will save in a file called WinForm.pas - a Pascal file.
The unit comprises two main parts - the interface section, which tells what the unit does. And an implementation section that holds the code that implements the interface.

Learning more ...
If you want to learn more, then you can view the main program file by selecting menu item Project/View source. This will show the code that Delphi generated to kick start your program. It includes a reference to WinForm.pas and your form, and starts your program running.
Unlike in previous Delphi releases, you can no longer see the form source code by right clicking the form.
Delphi Programming © Neil Moffatt All rights reserved.  |  Home Page