You are on page 1of 7

WINDOWS FORMS DEVELOPMENT ON .

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;

public class sampleform:Form

public static void Main()

sampleform f=new sampleform();

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.

public class sampleform:Form

The above statement creates the SampleForm class by inheriting the features of the Form base
class.

public static void Main()

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.

COMPILING the SampleForm.cs Program

To compile, come to the path where your file is stored and type csc filename.cs

To run  filename

CUSTOMIZING THE FORM

We can customize a form’s look and feel by making use of the various properties and methods of
the Form class.

Customizing the Caption Bar


The Form class supports a number of properties to enable the programmer to customize the
form’s caption as per his requirements. Some of these properties are:

 ControlBox : Enables or disables the control box.


 MaximizeBox : Enables or disables the maximize button.
 MinimizeBox : Enables or disables the minimize button.
 Text: Helps add a caption for the form.

//Program – SampleForm1.cs

using System.Windows.Forms;

using System.Drawing;

public class sampleform1:Form

public static void Main()

sampleform1 f=new sampleform1();

f.ControlBox=true;//control box referes to maximize, minimize and close buttons

f.MaximizeBox=false;//maximize button is disabled

f.MinimizeBox=true;

f.Text="Self study chapter";

Application.Run(f);

}
//Program – modified sampleform1.cs
using System.Windows.Forms;

using System.Drawing;

public class sampleform1:Form


{
public static void Main()
{
sampleform1 f=new sampleform1();
f.ControlBox=false;
f.MaximizeBox=false;
f.MinimizeBox=false;
f.Text="Self study chapter";
Application.Run(f);
}
}

Customizing the Size

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;

public class sampleform1:Form


{
public static void Main()
{
sampleform1 f=new sampleform1();

f.Height=125;
f.Width=250;

f.Text="Self study chapter";

Application.Run(f);
}
}

Customizing the Colors

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;

public class sampleform1:Form


{

public static void Main()

sampleform1 f=new sampleform1();

f.BackColor=Color.Pink;

f.Text="Self study chapter";

Application.Run(f);

Customizing the Borders

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:

 None : Removes the form’s border


 Sizable : Makes the form resizable.
 Fixed3D : Makes the form non resizable with a 3D border.
 FixedSingle : Makes the form non-resizable with a single line border.

//Program – borders.cs

using System.Windows.Forms;

using System.Drawing;

public class sampleform1:Form


{

public static void Main()

sampleform1 f=new sampleform1();

f.FormBorderStyle =FormBorderStyle.Sizable;

f.Text="Self study chapter";

Application.Run(f);

You might also like