VB Unit III
VB Unit III
Unit – III
Windows Application is really inappropriate as a project title, considering that it has the
ability to be compiled to IL and ported to another operating system where it can be
natively compiled.
Creating a
Window Forms
Application by
following the
following steps in
Microsoft Visual
Studio - File → New
Project → Windows
Forms Applications
Finally, select
OK, Microsoft Visual
Studio creates your
project and displays
following window
Form with a
name Form1.
WINDOWS FORMS
VB.Net programmers
have made extensive use of
forms to build user interfaces.
Each time you create a
Windows application, Visual
Studio will display a default
blank form, onto which you
can drag and drop controls
from the Visual Studio
Toolbox window.
The first step is to start
a new project and build a
form. Open your Visual Studio and select File->NewProject and select Visual Basic from
the New project dialog box and select Windows Froms Application.
Enter your project name instead of WindowsApplication1 in the bottom of dialouge box
and click OK button.
When you add a Windows Form to your project, many of the forms properties are set by
default.
Although these values are convenient, they will not always suit your programming needs.
The following picture shows how is the default Form look like.
At the top of the
form there is a title bar
which displays the forms
title.
Form1 is the
default name, you can
change the name to your
convenience.
The title bar also
includes the control box,
which holds the minimize,
maximize, and close
buttons.
If you want to set any properties of the Form, you can use Visual Studio Property
window to change it.
The RichTextBox control allows the user to display, enter, and edit text while also
providing more advanced formatting features than the conventional TextBox control.
Following is a general description of the RichTextBox (RTB) control along with
summaries of its pertinent properties, methods, and events; all compiled from the MSDN
help files.
The RichTextBox control provides a number of properties you can use to apply
formatting to any portion of text within the control.
To change the formatting of text, it must first be selected.
Only selected text can be assigned character and paragraph formatting.
Using these properties, you can make text bold or italic, change the color, and create
superscripts and subscripts.
You can also adjust paragraph formatting by setting both left and right indents, as well as
hanging indents.
The RichTextBox control opens and saves files in both the RTF format and regular
ASCII text format.
You can use methods of the control (LoadFile and SaveFile) to directly read and write
files, or use properties of the control such as SelRTF and TextRTF in conjunction with
Visual Basic's file input/output statements.
The RichTextBox control supports object embedding by using
the OLEObjects collection.
Each object inserted into the control is represented by an OLEObject object.
This allows you to create documents with the control that contain other documents or
objects.
For example, you can create a document that has an embedded Microsoft Excel
spreadsheet or a Microsoft Word document or any other OLE object registered on your
system.
To insert objects into the RichTextBox control, you simply drag a file or a highlighted
portion of a file used in another application (such as Microsoft Word), and drop the
contents directly onto the control.
The RichTextBox control supports both clipboard and OLE drag/drop of OLE objects.
CHECK BOXES
A GroupBox is a container control that places a border around its collection of controls.
It is often used to group radio buttons; but it is also a convenient way to organize and
manage any related controls on a form.
For example, setting the Enabled property of a group box to false disables all controls in
the group box.
o constructor: public GroupBox()
The constructor creates an untitled GroupBox having a default width of 200 pixels and a
default height of 100 pixels.
The Panel Class
The Panel control is a container used to group a collection of controls.
It’s closely related to the GroupBox control, but as a descendent of the ScrollableControl
class, it adds a scrolling capability.
o constructor: public Panel()
Its single constructor creates a borderless container area that has scrolling disabled.
This "no-frills" control has a single parameterless constructor and two properties worth
noting: a FlowDirection property that specifies the direction in which controls are to be
added to the container, and a WrapControls property that indicates whether child controls
are rendered on another row or truncated.
TableLayoutPanel Control
This code segment creates a TableLayoutPanel and adds the same four controls used in
the previous example.
Container properties are set to define a layout grid that has two rows, two columns, and
uses an Inset border style around each cell.
Controls are always added to the container moving left-to-right, top-to-bottom.
LIST BOXES
The CheckedListBox control gives you all the capability of a list box and also allows you
to display a check mark next to the items in the list box.
To add objects to the list at run time, assign an array of object references with the
AddRange method. The list then displays the default string value for each object.
o Dim days As String() = {"Sunday", "Monday", "Tuesday"}
o checkedListBox1.Items.AddRange(days)
You can add individual items to the list with the Add method. The CheckedListBox
object supports three states through the CheckState enumeration: Checked,
Indeterminate, and Unchecked.
o CheckedListBox1.Items.Add("Sunday", CheckState.Checked)
o CheckedListBox1.Items.Add("Monday", CheckState.Unchecked)
o CheckedListBox1.Items.Add("Tuesday", CheckState.Indeterminate)
COMBO BOXES
PICTURE BOXES
SCROLL BARS
A menu is located on the menu bar and contains a list of related commands. MainMenu is
the container for the Menu structure of the form and menus are made of MenuItem
objects that represent individual parts of a menu.
You can create a main menu object on your form using the MainMenu control. The
following picture shows how to drag the Menustrip Object to the Form.
DIALOG BOXES
All these above-mentioned classes have corresponding controls that could be added from
the Toolbox during design time.
When you double click any of the dialog controls in the toolbox or drag the control onto
the form, it appears in the Component tray at the bottom of the Windows Forms
Designer, they do not directly show up on the form.
The following table lists the commonly used dialog box controls. Click the following
links to check their detail –
The Font dialog appears, select a font and a color and click the OK button. The selected
font and color will be applied as the font and fore color of the text of the rich text box.
4. SaveFileDialog Control
The SaveFileDialog control
prompts the user to select a location for
saving a file and allows the user to specify
the name of the file to save data. The
SaveFileDialog control class inherits from
the abstract class FileDialog.
Following is the Save File dialog
box −
Properties of the SaveFileDialog
Control
The following are some of the
commonly used properties of the SaveFileDialog control −
Property & Description
AddExtension
Gets or sets a value indicating whether the dialog box automatically adds an extension to a
file name if the user omits the extension.
CheckFileExists
Gets or sets a value indicating whether the dialog box displays a warning if the user specifies
a file name that does not exist.
CheckPathExists
Gets or sets a value indicating whether the dialog box displays a warning if the user specifies
a path that does not exist.
DefaultExt
Gets or sets the default file name extension.
5. PrintDialog Control
The PrintDialog control lets the
user to print documents by selecting a
printer and choosing which sections of
the document to print from a Windows
Forms application.
There are various other
controls related to printing of
documents. Let us have a brief look at
these controls and their purpose. These
other controls are −
The PrintDocument control − it
provides support for actual events and
operations of printing in Visual Basic
and sets the properties for printing.
The PrinterSettings control − it
is used to configure how a document is printed by specifying the printer.
The PageSetUpDialog control − it allows the user to specify page-related print settings
including page orientation, paper size and margin size.
The PrintPreviewControl control − it represents the raw preview part of print previewing
from a Windows Forms application, without any dialog boxes or buttons.
The PrintPreviewDialog control − it represents a dialog box form that contains a
PrintPreviewControl for printing from a Windows Forms application.
Following is the Print dialog box −
Properties of the PrintDialog Control
The following are some of the commonly used properties of the PrintDialog control −
Property & Description
AllowCurrentPage
GRAPHICS
The main difference in these three controls is that the ImageBox control is
designed specifically for displaying images and not for creating new images
or manipulating them.
The other two controls provide drawing methods that let you design graphics at runtime.
The methods for loading graphics on the various controls are simpler than
creating graphics from scratch. You can place graphics oncontrols-at design time and,
runtime.
To load a graphic (bitmap or icon) on a control at design time, you assign its filename to
the Picture property of the control in the Properties window.
This same procedure can’t change the image displayed at runtime; instead, you must use
the LoadPictureO function, which is described shortly.
If the graphic is assigned to a control at design time, ‘it’s stored along with
the application. One of the files that the Visual Basic editor generates for each Form in a
project has the extension FRX.This is where the image (the actual bitmap) is stored.
As a consequence, the size of the application will increase if its Forms contain bitmaps
that must be loaded at runtime.
The alternative is to load the .graphic at runtime with the LoadPictureO function.