Windows Form

Windows Forms (or simply forms) are the windows you see in a Windows Application. You can create multiple forms in a single application. Each form inherits the properties and methods of the System.Windows.Forms.Form class. The namespace System.Windows.Forms contains components you will need for creating forms and controls.

The following are the parts of a typical windows form.

At the top, you will find the Caption Bar. The Caption Bar is composed of the icon, the caption, and the control box. The control box contains buttons such as   minimizing, maximizing, closing, or a help button. The Client Area is where we   add the controls. The border or frame, which includes the caption bar,encloses   the client area and allows you to resize the form.

The following are some of the useful properties of the Form base class.

Property Description
AcceptButton The button on the form that is pressed when you hit the Enter key.
CancelButton The button on the form that is pressed when you hit the Esc key.
ClientSize Gets or sets the client area of the form. The client area is the portion of the form inside the frame borders.
ControlBox Specifies whether to show the control box at the top right portion of the form. The control box contains the buttons minimize, maximize, and close.
Controls A collection of Control objects contained inside the form.
DesktopBounds The size and location of the form in the Window’s desktop.
Font The font that the form will use. Controls inside the form will inherit this property.
FormBorderStyle The border style of the form.
HelpButton Shows a help button right before the close button of the form. (minimize and maximize buttons should be disabled)
Icon The icon that will be used by the form.
Location The coordinates of the form in the screen.
MainMenuStrip Indicates the fundamental menu to be utilized by the shape.
MaximizeBox Tells whether the maximize box situated at the upper right is shown.
MinimizeBox Tells whether the minimize box located at the top right is displayed.
Modal Tells whether the form is modal.
Name The name of the form that is used to reference it in the code.
OwnedForms A collection of forms that this form owns.
Owner The form that owns this form.
ShowIcon Tells whether the icon is displayed at the left side of the caption bar.
Size The size of the form.
StartPosition The starting position of the form when it is initially shown.
Text The text that is shown in the caption bar of the form.

Figure 1

Figure 2 shows some useful methods of the Form class.

Method Description
Activate Gives the focus to this form and activates it.
AddOwnedForm Adds a new form that this form owns.
CenterToScreen Centers the position of the form in the screen.
Close Closes the form.
Hide Hides this form.
OnLoad Raises the Load event.
Show Shows the form.

Figure 2

Figure 3 shows the available events for the form.

Event Description
Activated Occurs when the form is activated.
Click Occurs when the form is clicked.
Deactivated Occurs when the form is no longer in focus.
FormClosed Occurs after the form is closed.
FormClosing Occurs when the form is closing. Allows you to halt the closing of the form.
HelpButtonClicked Occurs when the help button is clicked.
KeyPress Occurs when a key on the keyboard is pressed.
Load Occurs when the form is finished loading just before it is displayed.
MenuComplete Happens when the menu of the shape loses center.
MenuStart Happens when the menu of the frame gets center.
ResizeBegin Occurs when the form enters resizing mode.
ResizeEnd Occurs when the form exits resizing mode.
Shown Occurs after the form is shown for the first time.

Figure 3

The Form class is a child of the System.Windows.Forms.Control base class so the methods and properties from the Control class are also available in the Form class.

Modifying the Control Box

We use the ControlBox property to hide or show the Control Box. This is useful when you are planning to disable minimizing or   maximizing of control or you want to only close the form through the code. The image   below shows you how the form will look when you set  ControlBox property to false.

If you want to disable only the minimize or the maximize button, then you can use the MinimizeBox and  MaximizeBox and set them to false.

The form above has its minimize and maximize box hidden. Unfortunately, you cannot hide only the close button.

Changing Form’s Border Style

We can change the border style of the form. For example, let’s say you don’t want   the user to be able to resize the form The default border of the form allow a   user to do that. We can set the FormBorderStyle   property to different values of the  System.Windows.Forms.FormBorderStyle Enumeration.

 Value  Description
 None  The form has no border.
 FixedSingle  The form has a non-resizable single line border.
 Fixed3D  The shape has a non-resizable 3d fringe.
 FixedDialog The frame has a thick, non-resizable, dialog style fringe that has no limit or   maximize boxes.
 Sizable  The default. The form has a resizable border.
 FixedToolWindow  The form encompasses a non-resizable border that has solely an in depth button. This vogue is used for tool windows.
 SizableToolWindow  Same as FixedToolWindow but resizable.

The following are screenshots of forms using different FormBorderStyle.








Form Icons

We use the Icon property to change the icon displayed at the upper left side of the form. Click the browse button next the   Icon property in the Properties Window and find the .ico file which is the file extension for an icon image. The ShowIcon property   allows you to hide or show the icon in the caption bar.

Accept and Cancel Buttons

You can add a button control to the form and set them as either an Accept or a   Cancel button. You do that using the AcceptButton and CancelButton properties. If a button is an accept button, whenever the user hits Enter while the form is active, that   button’s Clickevent will be executed. The Cancel   button is activated whenever the Escape key is pressed. Just go to the   Properties Window, find the desired property and click the drop down button. You   will be presented with the names of all the button control in the form. Choose   the desired button. For example, suppose you are creating a login form. You can   set the button used for logging in as the Accept button. This way, the user can simply press Enter when he is finished typing the password.

There are many more to discover on windows forms and they will be discussed in   later lessons.