Windows
Forms
Windows
forms provide many enhancements over standard VB forms including:
Rich set of Controls: By using the
classes in the System.Windows.Forms namespace, you can create windows
applications that take full advantage of the rich user interface features
available in Microsoft Windows Operating System. This namespace provides “Form”
class and many other controls that can be added to the form to create user
interface.
Advanced Printing Support: .net provides
different controls and dialog boxes to handle printing options. Ex:
PrintSetupDialog, PrintPreviewDialog, PrintPreviewControl etc.
Advanced Graphics Support: we can design 2D
graphics with the use of namespaces like System.Drawing,
System.Drawing.Drawing2D etc.
Visual Inheritance: windows forms are
classes and can benefit from inheritance. Windows forms can be inherited from
derived form that automatically inherits the controls and code defined by base
forms.
Form:
A form is the window that is used to
present information to or accept information from the end use. The form can be
a single window, and MDI window or a Dialog Box.
A
form will have two windows. Designer and code
The
Windows Forms Designer:
- Contains comments in XML Documentation
- A Container object is required if you need to
group the objects together.
The advantage of this object is,
even though there may be elements in the application that are not controls, the
“Container” object allows us to group everything together.
Private
System.ComponentModel.Container Components=NULL;
- The Dispose() method is used to perform any
operation that are necessary when garbage collection is done.
- In the InitializeComponent() method, the controls
are instantiated and are set to their initial behavior.
- Then Main() method specifies the starting point of
the application.
Form Properties:
Property
|
Description
|
AcceptButton
|
Specifies
which buttons click event has to be executed when the user presses enter key
|
CancelButton
|
Specifies
which buttons click event has to be executed when the user presses ‘Esc’ key
|
BackColor
|
Sets
the forms back color
|
ControlBox
|
True/false
to display min,max,close controls
|
FormBorderStyle
|
Specifies
how the form border look like
|
StartPosition
|
Specifies
at which position of the desktop the form has to be displayed.
|
Form Events:
Certain events are triggered as a form
is opened, closed, moved and so on.
Event
|
Description
|
Load
|
Triggers
just before the form is loaded into the memory. It happens with Load()/Show()
methods
|
Activated
|
When
a form is activated in code or becomes visible through user movement amongst
forms.
|
Deactivated
|
It
is caused when the form loses focus
|
Closing
|
To
examine the method in which the used
closed the form
|
Closed
|
Just
prior to dispose event this event occurs, you could verity the closing
intensions
|
Dispose
|
There
is no terminate event in .net. it is used to provide any finalization code.
|
Windows Controls:
The System.Windows.Forms.Control class
is the base class for all controls that are to be created. This class is
derived from System.ComponentModel.Component class.
Hierarchy
of Control classes:
- System.ComponentModel.Component
- Control
- ButtonBase
- Button
- ScrollableControl
- ContainerControl
- Form
Types of Controls:
Type
|
Controls
|
Command
|
Button,
LinkLabel, NofityIcon, ToolBar
|
Menu
|
MainMenu,
ContextMenu
|
Selection
|
CheckedListBox,
ComboBox, ListBox, ListView, TreeView
|
Text
|
TextBox,
RitchTextBox, Label, LinkLabel, StatusBar
|
Graphics
|
PictureBox,
ImageList
|
Value
|
CheckBox,
CheckedListBox, RadioButton
|
Date
|
DateTimePicker,
MonthCalander
|
Grouping
|
Panel, GroupBox,
TabControl
|
CommonDialog
|
Color, Font,
OpenFile, SaveFile, Print, PrintPreview
|
Windows
Controls:
Control
|
Description
|
Button
|
Control that can be clicked to perform
some desired action.
|
CheckBox
|
Primarily used for displaying a binary
state of an object. Clicking the check box causes it to toggle between a
checked or unchecked state.
|
CheckListBox
|
List box with a column of check boxes.
|
ComboBox
|
A drop-down list of choices that operates
similar to the list box. The primary difference is that the combo box is more
compact and efficient with screen real estate.
|
DataGridView
|
An extremely powerful control that
permits a program to bind to a data source.
|
DateTimePicker
|
Provides a capability to select a date
and time without typing.
|
DomainUpDown
|
Permits a user to scroll through a list
of data items that can only be shown one at a time.
|
Form
|
The main window of an application, a
dialog, or a multiple-document interface (MDI) child. It provides all the
capabilities for hosting child controls.
|
GroupBox
|
Houses a group of other controls often
used to encapsulate a group of radio buttons. It can help organize a form and
has a customizable title.
|
Label
|
Primarily used to display static text but
can also contain
Images.
|
LinkLabel
|
The same as a label, but it can contain
an URL that can be clicked to invoke an Internet connection.
|
ListBox
|
Holds selectable lists of data items.
When the viewable portion of the list box is filled, a scrollbar appears so
that all of its contained items may be selected.
|
ListView
|
Provides capabilities for multiple
columns, column headers, column resizing, and list sorting It can also be
configured in four different display modes. More Sophisticated than a list
box.
|
MessageBox
|
Provides notifications to users on
certain program events. It has a configurable message, title bar, icon, and
button.
|
MonthCalendar
|
A visual calendar control.
|
NumericUpDown
|
The same as a DomainUpDown, with the
restriction that its
contents are
numeric.
|
Panel
|
Blank forms with little or no decoration
that are used primarily for organization and form layout.
|
PictureBox
|
Displays an image.
|
ProgressBar
|
Used to display the status of an
ongoing operation. It has a graph-ical indicator, set by a program to show
the percentage of task completion.
|
RadioButton
|
Mutually exclusive buttons that
permit users to make a choice. Also called option buttons.
|
RichTextBox
|
An enhanced text box control that
provides more control over its text. It has the capability of creating Rich
Text Format (RTF) files.
|
ScrollBar
|
Often used to help position the
current location in a document
That’s too large
to fit onscreen or in whatever space is available.
|
Splitter
|
Permit a user to resize multiple
portions of a workspace. When the splitter is moved, one portion of the
workspace gets larger, and others become smaller.
|
StatusBar
|
Performs multiple functions.
Primarily it’s a place to notify users of a program’s status or other forms
of current information.
|
TabControl
|
User interfaces that appear like file
folder tabs. When selected, they open a specific page where the tab and
content match.
|
TextBox
|
Allows a user to type text. They can
be single line or multiline and have many capabilities for text manipulation
such as selection, cut, copy, and paste.
|
Timer
|
Non visual controls that raise events
at specified intervals. They can be used for such things as reminders or
auto-save operations.
|
ToolBar
|
Permits a user to invoke selected
operations in a program; similar in functionality to menus.
|
ToolTip
|
Helpful messages that appear when a
cursor hovers over a control for a specified amount of time.
|
TrayIcon
|
Icons displayed on the icon tray of
the window’s taskbar. They usually have different pictures to indicate the
current state of a program.
|
TreeView
|
Displays items in a hierarchical
fashion. It has a root node at the top of the tree and can have multiple
branches and nodes. Traditionally, it has collapsible branches and is
coordinated with another control to display details of selected nodes.
|
Menus
Most applications have a main menu, which is a
quick way to access all the functionality of an application. Another popular
feature of programs is to offer context-sensitive menus via right-click on
parts of the program where they might make sense.
You can have more than one menu system per
form which reduces the complexity of creating dynamic menus, and you can create
context menus directly without designing them as top level menus first.
Menu
Classes:
Three menu classes
are there that will use when creating menus.
MainMenu: you use this class to
create a standard windows associated menu bar at the top of a form.
ContextMenu: You use this class to define pop-up menus associated with
particular controls
MenuItem: You use this class to define menu items within a MainMenu or a
ContextMenu.
Creating a MainMenu at Design Time:
When we add the MainMenu control it
will be placed in the component tray. Once an instance of the control becomes
part of your application, you will be presented with the first menu item a
prompt to “Type Here”. The menu control leaves open space for us to add to the
menu system at any time.
- To create a menu separator, type a “hyphen(-)” in
the “type here” prompt.
- If you want to create sub menus, simply type in
“type here” area that is displayed to the right of the current area. The
control will add the appropriate arrows to indicate a sub menu.
- The main menu is made up of MenuItem children
objects. The hierarchy is then created between parent and child objects.
- To create an access key for a menu item, select
the menu item to which you would like to assign an access key. Type an
“ampersand(&)” in-front of the letter that is to be underlined and is
to function as the access key.
- To assign a shortcut key in main menu system,
select the menu item, access the properties window and select the
“Shortcut” property. Select the appropriate shortcut key combination from
the drop-down menu that appears.
Note:
To suppress the display of the shortcut key in the menu, set the “showShortcut”
property of the menu item to false. Other properties for the menu item are:
Ø
Checked
– displays the menu item with a check mark.
Ø
DefaultItem
– specifies the menu item as the default.
Ø
Enabled
– Grays out the menu item and makes it inaccessible.
Creating
Menus Programmatically:
This example programmatically creates a Windows Forms
ContextMenu and associates it with a control.
private void Form1_Load(object sender, System.EventArgs e)
{
System.Windows.Forms.ContextMenu contextMenu1;
contextMenu1 = new
System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem menuItem1;
menuItem1 = new
System.Windows.Forms.MenuItem();
System.Windows.Forms.MenuItem menuItem2;
menuItem2 = new
System.Windows.Forms.MenuItem();
System.Windows.Forms.MenuItem menuItem3;
menuItem3 = new
System.Windows.Forms.MenuItem();
contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]
{menuItem1, menuItem2, menuItem3});
menuItem1.Index = 0;
menuItem1.Text =
"MenuItem1";
menuItem2.Index = 1;
menuItem2.Text = "MenuItem2";
menuItem3.Index = 2;
menuItem3.Text =
"MenuItem3";
textBox1.ContextMenu =
contextMenu1;
}
MDI Forms
Multiple-Document
Interface(MDI) applications allow you to display multiple documents at the same
time with each document displayed in its own window under a container.
Creating a Parent Form:
You can use the
“IsMdiContainer” property of a form to make it an MDI Parent form. This holds a
Boolean value.
Creating Child Forms:
You can create
child forms by setting the “MdiParent” property of a form to the name of the
already created MDI parent.
Sub AddDoc()
Form2 frm=new Form2();
Frm.MdiParemt=this;
Frm.Show();
No comments:
Post a Comment