Creating Windows Forms Form Is The Very First Entity Included in Windows-Based Applications. at Runtime, A Form
Creating Windows Forms Form Is The Very First Entity Included in Windows-Based Applications. at Runtime, A Form
NET
CREATING WINDOWS FORMS
Form is the very first entity included in windows-based applications. At runtime, a form
continuously waits for an event to occur, such as the clicking of the mouse or pressing of a key.
As soon as an event occurs, it triggers the corresponding event-handling code.
In c#, a form can be created by inheriting the Form class contained in the
System.Windows.Forms namespace.
//Program-SampleForm.cs
using System.Windows.Forms;
Application.Run(f);
Note: In the above code, a Windows form named SampleForm has been created by inheriting the
Form base class.
using System.Windows.Forms;
The above statement adds the System.Windows.Forms namespace to the program. It contains
the Form and Application classes that are used in the program.
The above statement creates the SampleForm class by inheriting the features of the Form base
class.
The above statement defines the main method indicating to the compiler the starting point of the
program.
sampleform f=new sampleform(); This statement instantiates the SampleForm class by creating
the f object.
Application.Run(f); calls the Run method of the Application class, which engages the program
into a continuous loop; servicing the user initiated events in between.
Application.Run method puts the program in a continuous loop making the form object wait for
an event to occur. As soon as an event occurs, the corresponding event handler is notified to
perform the desired operation.
In case of f form object that we created earlier, there are no controls added on the forms
body.Thus there is no possibility of event occurrence. But there are 3 default buttons or controls
on the title bar these are maximize, minimize and close. Thus as soon as user clicks any one of
these buttons,an event is raised and the corresponding event handling code is executed.This is the
only way for us to bring an end to the program and break its loop.
To compile, come to the path where your file is stored and type csc filename.cs
To run filename
We can customize a form’s look and feel by making use of the various properties and methods of
the Form class.
//Program – SampleForm1.cs
using System.Windows.Forms;
using System.Drawing;
f.MinimizeBox=true;
Application.Run(f);
}
//Program – modified sampleform1.cs
using System.Windows.Forms;
using System.Drawing;
The Form class supports a number of properties to enable the programmer to specify the size
related settings of a form. Some of these properties are:
DefaultSize : Sets the default size of a form.
Height : Sets the height of a form.
Width : Sets the wodth of a form.
MaximumSize : Sets the maximum size of a form.
MinimumSize : Sets the minimum size of a form.
StartPosition : Helps specify the initial position of the form.
//Program size.cs
using System.Windows.Forms;
using System.Drawing;
f.Height=125;
f.Width=250;
Application.Run(f);
}
}
The BackColor property of the Form class enables us to modify the background color of a form.
The choice of the colors can be made from the color structure contained in the System.Drawing
namespace.
//Program- back_color.cs
using System.Windows.Forms;
using System.Drawing;
f.BackColor=Color.Pink;
Application.Run(f);
We can customize the border of a form by making use of the FormBorderStyle property of the
Form class. The FormBorderStyle property allows us to not only change the border style but it
also enables us to configure the resizing capability of a form. Some of the values that
FormBorderStyle property can assume are the following:
//Program – borders.cs
using System.Windows.Forms;
using System.Drawing;
f.FormBorderStyle =FormBorderStyle.Sizable;
Application.Run(f);