Introduction:
In C#, the form is the
container for all the controls that make up the user interface. When a C#
application is executing, each window it displays on the desktop is a form.
Every C# application starts with
the form. Forms have built-in functionality that is always available without
any programming effort. You can move a form around, resize it, and even cover
it with other forms. You can do this with the mouse, or with the keyboard
through the Control menu.
The forms that compose the visible interface of your application are called Windows forms. A dialog box is a form with a small number of controls, no menus, and usually an OK and a Cancel button to close it.
The Appearance of the Form
Applications are made up of one or more forms, and the
forms are what users see. A windows form can be viewed as shown in figure.
|
Property |
Description |
|
BackColor |
This property is used to sets or to get the background
color for this form. |
|
BackgroundImage |
This property is used to sets or to get the background
image in the form. |
|
ControlBox |
This is a Boolean property, having one of values TRUE or
FALSE. TRUE value indicates that Control Box is displayed and active, where
as FALSE value indicates that Control Box is get removed form the form. |
|
Enabled |
This is a Boolean property, having one of values TRUE or
FALSE. TRUE means the form is Enabled i.e. all controls on form are in
working. FALSE means all control on the form are not in working. |
|
ForeColor |
This property is used to set forecolor to the form. |
|
FormBorderStyle |
This property determines the
style of the form’s border and the appearance of the form. This property has different property values some of the property values are Property Value Purpose None Borderless window that can’t be resized; this setting should be avoided. Sizable (default) Resizable window that’s used for
displaying regular forms. Fixed3D Window with a visible border, “raised” relative to the main area. Can’t be resized. |
|
MaximizeBox |
Is used to active or inactive the Maximize button on
Title bar. It is Boolean Property. |
|
MinimizeBox |
Is used to active or inactive the Minimize button on
Title bar. It is Boolean Property. |
|
Name |
This property is used to give name to the form. This
property value is being used while writing a program. |
|
ShowInTaskbar |
Is used to decide whether a form is to be display on the
Taskbar or not. It is Boolean Property. |
|
StartPosition |
This property determines the
initial position of the form when it’s first displayed; it can have one of the
following values. Value Effect CenterParent The form is centered
in the area of its parent form. CenterScreen The form is centered
on the monitor. Manual The location and size of the form
will determine its starting position. WindowsDefaultBounds The form is positioned at the default location and size determined by Windows. WindowsDefaultLocation The form is positioned at the Windows default location and has the dimensions you’ve set at design time. |
|
Text |
This property is used to Display name of Form on Title
bar for user interaction purpose. |
|
WindowState |
This property is used to determine the state of window
form while it is executing. This property has different property values. |
One of the most tedious tasks in designing user interfaces with C# was the proper arrangement of the controls on the form, especially on forms that users were allowed to resize at runtime. You design a nice form for a given size, and when it’s resized at runtime, the controls are all clustered in the top-left corner. A TextBox control that covered the entire width of the form at design time suddenly shrink on the left when the user drags out the window.
The Anchor and Dock properties of the various controls allow you specify how they will be arranged with respect to the edges of the form when the user resizes it.
The Anchor property lets you attach one or more edges of the control to corresponding edges of the form. The anchored edges of the control maintain the same distance from the corresponding edges of the form. Place a TextBox control on a new form and then open the control’s Anchor property in the Properties window. You will see a little square within a larger square, and four pegs (hanger) that connect the small control to the sides of the larger box. The large box is the form, and the small one is the control. The four pegs are the anchors, which can be either white or gray. The gray anchors denote a fixed distance between the control and the form. By default, the control is placed at a fixed distance from the top-left corner of the form. When the form is resized, the control retains its size and its distance from the top-left corner of the form.
In addition to the Anchor property, most controls provide the Dock property, which determines how a control will dock on the form. The default value of this property is None.
Create a new form, place a TextBox control on it, and then
open the control’s Dock property. The various rectangular shapes are the
settings of the property. If you click the middle rectangle, the control will
be docked over the entire form: it will expand and shrink both horizontally and
vertically to cover the entire form. This setting is appropriate for simple
forms that contain a single control, usually a TextBox, and sometimes a menu.
The Form
Events
Event is an action
performed by user, e.g. moving a mouse, pressing a key on keyboard, selecting a
control on form.
C# supports
several event functions depending upon the user’s action. As a form can be
minimized, maximized, closed, activated and so on, to handle all these actions
of a form C# supports several methods k/as events of a form.
Following are some
of the important events of a form.
1) Load Event :
This event occurs
before a form is displayed for the first time. This
is default event of a form. This event is used to perform buildup operation on
variables. This event is fired when the user executes the form by clicking on
run button or by pressing F5 key from keyboard. When this event fires it loads
all necessary items required in to memory.
The general form of load event is
private void Form1_Load(object sender, EventArgs
e)
{
}
2) Activated:
This event occurs
when the form is activated in code or by the user. This event occurred
immediately after the Load event.
The general form of
this event is
private void Form1_Activated(object sender, EventArgs
e)
{
}
3) Click :
This event occurs
when the form is clicked by user.
private void Form1_Click(object sender, EventArgs
e)
{
}
5) FormClosing :
This event is
fired when the user closes the form by clicking its Close button and the
application is in closing procedure.
private void Form1_FormClosing(object sender, FormClosingEventArgs
e)
{
}
4) FormClosed :
This event is
fired when the user closes the form by clicking its Close button and the
application is closed.
private void Form1_FormClosed(object sender, FormClosedEventArgs
e)
{
}
Loading
and Showing Forms
One of the operations you’ll have to perform with
multi-form applications is to load and manipulate forms from within other
forms’ code. To access a form from within another form, you must first create a
variable that references the second form. If your application has two forms,
named Form1 and Form2, and that Form1 is the project’s startup form. To show
Form2 when an action takes place on Form1, first declare a variable that
references Form2:
Form1 frm = new Form1();
This declaration must appear in
Form1 and must be placed outside any procedure. Then, to invoke Form2 from
within Form1, execute the following statement:
frm.Show();
This statement will bring up
Form2 and usually appears in a button’s or menu item’s Click event handler. At
this point, the two forms don’t communicate with one another. However, they’re
both on the desktop and you can switch between them. There’s no mechanism to
move information from Form2 back to Form1
The Show method opens Form2 in a modeless manner. The two forms are equal in stature on the desktop, and the user can switch between them. You can also display the second form in a modal manner, which means that users won’t be able to return to the form from which they invoked it.
Controlling
One Form from within Another
Loading and displaying a
form from within another form’s code is fairly small. In some situations, this
is all the interaction you need between forms. Each form is designed to operate
independently of the others, but they can communicate via public variables.
In most situations, however, you need to control one form
from within another’s code. Controlling the form means accessing its controls
and setting or reading values from within another form’s code.
Designing
Menus
Menus are one of the most common and characteristic elements of the Windows user interface. Even in the old days of character-based displays, menus were used to display methodically organized choices and guide the user through an application. Despite the visually rich interfaces of Windows applications and the many alternatives, menus are still the most popular means of organizing a large number of options. Many applications duplicate some or all of their menus in the form of toolbar icons, but the menu is a standard fixture of a form. You can turn the toolbars on and off, but not the menus.
The Menu Editor
Menus can be attached only to forms, and they’re
implemented through the MainMenu control. The items that make up
the menu are MenuItem objects. The MainMenu control and MenuItem
objects give you absolute control over the structure and appearance of the
menus of your application.
The IDE provides a visual tool for designing menus, and
then you can program their Click event handlers. In principle, that’s all there
is to a menu: you design it, then you program each command’s actions. Depending
on the needs of your application, you may wish to enable and disable certain commands,
add context menus to some of the controls on your form, and so on. Because each
item (command) in a menu is represented by a MenuItem object, you can control
the application’s menus from within your code by manipulating the properties of
the MenuItem objects.
Let’s
start by designing a simple menu :
Ø
Double-click the MenuStrip
icon on the Toolbox.
Ø
The MenuStrip control
will be added to the form.
Ø
A single menu command
will appear on your form.
Ø Its caption will be Type Here.
If
you don’t see the first menu item on the Form right away, select the MenuStrip
control in the Components tray below the form. Do as the caption says.
Click it and enter the first command’s caption, File.
As soon as you start typing, two more captions appear: one on the same level
(the second command of the form’s main menu, representing the second pull-down
menu) and another one below File (representing the first command on the File
menu). Select the item under File and enter the string New.
Enter the remaining items of the File menu—Open, Save, and Exit—and then click somewhere on the form. All the temporary items (the ones with the Type Here caption) will disappear, and the menu will be finalized on the form. At any point, you can add more items by right-clicking one of the existing menu items and selecting Insert New.
To add the Edit menu, select the MenuStrip icon to activate the visual menu editor and then click the File item. In the new item that appears next to that, enter the string Edit. Press Enter and you’ll switch to the first item of the Edit menu. Fill the Edit menu with the commands.
Each menu item has a name, which allows you to access the properties of the menu item from within your code. The same name is also used in naming the Click event handler of the item. The default names of the menu items you add visually to the application’s menu are MenuItem1, MenuItem2, and so on. To change the default names to something more meaningful, you can change the Name property in the Properties window. To view the properties of a menu item, select it with the left mouse button, then right click it and select Properties from the context menu.
To create a separator bar in a menu, right-click the item you want to display below the separator and select Insert Separator. Separator bars divide menu items into logical groups, and even though they have the structure of regular menu commands, they don’t react to the mouse click. You can also create a separator bar by setting the item’s caption to a dash (-).
As you will notice, the menus expand by default to the bottom and to the right. To insert a menu command to the left of an existing command, or to insert a menu item above an existing menu item, right-click the item following the one you want to insert and select Insert New.
Programming Menu Commands
Menu commands are similar to controls. They have certain properties that you can manipulate from within your code, and they trigger a Click event when they’re clicked with the mouse or selected with the Enter key. If you double-click a menu command at design time, C# opens the code for the Click event in the code window. The name of the event handler for the Click event is composed of the command’s name followed by an underscore character and the event’s name, as with all other controls.
To program a menu item, insert the appropriate code in the MenuItem’s Click event handler. A related event is the Select event, which is fired when the cursor is placed over a menu item, even if it’s not clicked.
The New command’s code would be something like:
private void newToolStripMenuItem_Click(object
sender, EventArgs e)
{
}
All you really need to know is that each menu item is a MenuItem object, and it fires the Click event every time it’s selected with the mouse or the keyboard. In most cases, you can treat the Click event handler of a MenuItem object just like the Click event handler of a Button.
Using
Access and Shortcut Keys
Menus are a convenient way of displaying a large number of choices to the user. They allow you to organize commands in groups, according to their function, and are available at all times. Opening menus and selecting commands with the mouse, however, can be an inconvenience. When using a word processor, for example, you don’t want to have to take your hands off the keyboard and reach for the mouse. To simplify menu access, C# supports shortcut keys.
Shortcut Keys
Shortcut keys are similar to access keys, but instead of
opening a menu, they run a command when pressed. Assign shortcut keys to
frequently used menu commands, so that users can reach them with a single
keystroke. Shortcut keys are combinations of the Ctrl key and a function or
character key.
For example, the usual access key for the Close command (once the File menu is opened with Alt+F) is C; but the usual shortcut key for the Close command is Ctrl+W.
To assign a shortcut key to a menu command, drop down the Shortcut list in the MenuItem’s Properties window and select a keystroke. You don’t have to insert any special characters in the command’s caption, nor do you have to enter the keystroke next to the caption. To view the possible keystrokes you can use as shortcuts, select a MenuItem in the Form Designer and expand the drop-down list of the Shortcut property in the Properties window. When assigning access and shortcut keys, take into consideration well-established Windows standards.
MDI Applications: MDI applications must have at least two forms, the parent form and one or more child forms. There may be many child forms contained within the parent form, but there can be only one parent form. The parent form is the MDI form, or MDI container, because it contains all child forms. The parent form may not contain any controls expect MinuStrip, ToolStrip and StatusStrip comtrol.
Creating
an MDI form is a simple, only you have to set one property of a from to true.
Ex: IsMdiContainer =
True
The form whose IsMdiContainer property is set to True, the said
form is called as MDI Container / MDI Parent form.
To Create MDI
Child form, following line of code is used.
private void newToolStripMenuItem_Click(object
sender, EventArgs e)
{
Form2 f
= new Form2();
f.MdiParent = this;
f.Show();
}
In above peace of code,
1)
Form2 f = new Form2(); is used to create an instance of new form.
2)
f.MdiParent = this; statement is used to
assign the current working form as MdiParent using keyword this.
3)
f.Show(); is used to show the MdiChild form.


