Nothing Special   »   [go: up one dir, main page]

VB - 2 Unit

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 13

Unit — II

THE TOOL BOX

Pointer

Picture Box

You want to add


Label Box more controls in the
tool box
Text Box
Right click on the
tool box
Frame
Select
Command Button
Components
Select the tool
Check Box
Check box checked
Option Button Click Apply
Click Ok
Combo Box

List Box The selected tool


will be appear in
Horizontal Scroll Bar
the Tool
Vertical Scroll Bar
Box

Timer

Drive List Box

Directory List Box

File List Box

Shape Control

Line Control

Image Control

Data Control

OLE
The tool box is one of the window in the visual basic desktop. It contains the standard
controls.
The standard controls found in the tool box are Picture Box, Labels, textbox Frames,
Command Button, Check Box, Option Button, Combo Box, List Box, Horizontal scroll bars, Vertical
scroll bar, Timer, Drive List box, Directory Listbox, File listbox, Shape. Line, Image data and OLE.
Apart from these standard controls, there are many other controls like DB combo, Dblist etc..
These advanced controls can be added to the toolbox in two ways.
• By selecting the components command from the project menu.
• By right clicking the mouse on the toolbox and (hen selecting the components command.
There are various components in the list we choose whatever we want by clicking on the text
box listed on the side of each one and finally finish by ok button to be appeared on the tool box.

CREATING CONTROLS
The objects available in the toolbox are called controls. Using these controls, the user can
interact with the application program and control the application program.
We can always get a control on a form by double clicking on its icon in the toolbox. This
gives you control in its default size and shape in the size of the form. This is certainly one way to
proceed.
Another way to bring the control to the form is by clicking the selected tool on the toolbox,
then move the mouse pointer to the form and start drawing through click to the required size of the
user
Each control has eight little boxes, called sizing handles. We can use these handles to move
and resize a control after you have created it.
We can use Cut, Copy, and Paste on the controls, Moving of control from one place to other
place of the form is possible. There are possible for locking the controls. That is once placed cannot be
moved when the control is not need we can also Delete the controls.

THE NAME PROPERTY


The name property is used to identify a control in the program. VB assign a default name to
each control placed on a form in an order for exempt The first TextBox placed m a form has the name
Text 1, the second Text2 and so on. The Developer can change the name of the controls through the
property window.
The name of a control must be meaningful in the sense that from the name itself the developer
can identify the purpose and type of the control. For example the name of the command button starts
with Cmd, textbox with Txt, Comobo with Cmb, Listbox with ist and so on. The remaining part of the
name may start with a capital letter and give a meaningful name. For example TexInterest,
Cmdlnterest, Cmd exit etc..
The limitation on control name are
* The name must begin with a letter
* After the first letter, we can use any combination of letters, digits and underscores
• The name cannot be longer than 40 characters.
PROPERTIES OF COMMAND BUTTON
The command Button is used to perform a f when the user clicks it. The property window of a
command button are a follows.
Caption property
The caption property on a command button determines what the user sees on the face of the
button it can use any font style.
Visible
This property determines whether the command button is visible or not, the value set to
True/False make command button visible and invisible depends on the situation of the user needs to
separate on the form.
Enabled
This property determines whether the button can respond to any event. (f you set this to false.
VB wilt not respond to any event for that button.
Picture
This property set the command button with the picture for move easy.
Understanding this are of three types.
I. Disable Picture - This shows the picture if button is disable
2. Down Picture — when the mouse is clicked this shows the picture.
3. Picture — Set the specific picture on the button as background.
Font
This is used for the caption of the button to choose any font style. We can use only one font at a time.
Height,Width
This expands or shortens the height and with of the button as needed.
Left,Top
Keeps a specific distance between the controls.

Mouse pointer
Sets different mouse pointer from the available pointers.

ACCESS KEYS:
Many window applications allow using ALT and other keys as a shortcut key called “The
Access Key”, to quickly activate a control or to a menu item itself. These access keys are underlined
in the caption of the control or name of the menu item.
VB makes it easy to set up an access key for any object that has a caption property. When you
set the caption, all you have to do is place an ampersand (&) in front of the letter you want td make it
as an Access Key.
Although it is possible to have the same access key for more than one control on a form, doing
so is unusual. The focus moves to the next control with the same access key. but that control is not
activated until you click the mouse or press the spacebar.

Navigator Between Controls


Mouse is the most common way to move from control to control in a windows application, but
your applications have to allow for using the tab key as well. Tab order is the term used in a windows
application for the sequence of controls that pressing tab moves you through control. In a VB
application, the order in which you create the control is the order used for the tab order. The first
control you create at design time is the one that receive the focus when the application starts. If you
press Tab once when the application is running, you move to the second control you created at design
time and so on.
The property you need to set is the Tab index property. If you set this value to 0
for a control, this will automatically becomes the first control in tab order, and all the other controls
move upward in tab order.

THE GRID
The grid is important for accurately positioning of controls. In order to control the grid, choose
tools/option and then go to the general page on the options dialog box.

Properties of The Grid


The 4 properties you can control are described below:
Show Grid
You can turn the grid on or off by changing the show grid setting. The default setting is on.
There is usually little reason to turn the grid off.
Grid Width Grid Height Boxes
To align controls to grid check box determines whether controls automatically move to the
next grid mark or whether they can be placed between the grid marks. Usually you are better off
changing the grid spacing to match your design requirements that turning this option off
RICH TEST BOXES:
The rich text box control is a custom control. You will need to add it to the
Tool bar if it is not already there. To do this
— Choose project/components to open the component dialog box
— Choose Microsoft Rich Text Box Control 6.0
— The icon for the rich text box
The rich textbox control lets you display text with multiple fonts and sizes
without having to go out and buy a third-party custom control. Moreover, the rich text
box is not limited to 32k characters like an ordinary text box.

Properties of Rich Text Box


Sel Length, Sel Start, Sel Text
Property What it Does
Set Length Return or sets the number of characters selected.
S Start Returns sets the starting point of the selected text
Set Text Returns or sets a string equal to the currently selected text.

Sel Bold, Sel Italic, Sel Strikethrou Sel Underline


To change the currently selected text to Hold, in rich text box using the default name of rich
text box1, might use.
RichTextBox1.SelBold = True
SelColor
This property sets the color of the currently selected text and of all text added after the current
insertion point.
Eg
RichTextBox1.SelColor = vbRed
SelFontName

This property lets you to change the font styles that are being selected by the users.
Eg
RichTextBox1.SelFontName = "Times New Roman"
SelFontSize

This property lets you to change the font size of the currently selected text, the syntax is
RichTextBox1.SelFontSize = 14

IMAGE CONTROL
Image controls can be used to display icons or pictures with the program such as Microsoft
Paintbrush. They can hold window BMP, GIF metafiles or JPEG. JPEG format is also the common
format used by most digital cameras. Since the image controls respond to the click event, you can use
these images to substitute for the command buttons.

You can load a picture into an image control by resulting the value of the
picture property. If you choose the picture property for the image control, this opens
up a dialog box that lets you choose what image file to load.

You can also reset the picture property directly by copying an image from a graphics program to the
windows clipboard and then using the Copy command on the edit menu to part the image into the
image control. VB will attach the graphics to the project when you save it. Finally, you can change
this property.

Properties of Image Control


Many of the properties of the image controls are similar to those for command buttons such as
left, top, etc., the border style property for an image control has only two possible settings, you can
either have no border or fixed style of border.
The most important property of an image control that haven’t yet seen is the stretch property.
This determines whether the image control adjusts to fit the picture, or the picture adjusts to fit on the
control. If the stretch is left at the default value of false. The control resizes itself to fit the picture. If
you change it True, the picture resize to fit the control.
MESSAGE BOX
Message box should be used for short messages or to give transient feedback. They wait for
the user to choose a button before returning to the application as long as VB is displaying a message
box. The simplest form of the message box command looks like this.
Msgbox “The Message Goes in Quotes”
Message boxes can hold a maximum of 1024 characters and VB automatically breaks the lines
at the right side of the dialog box.
You can add your own, more informative title to a message box. For this, you have to use the
form of the message box statement by adding two options to it.
Syntax

MsgBox(prompt[, buttons] [, title] [, helpfile, context])

Example:

MsgBox "Welcome to VB", vbYesNoCancel + vbExclamation + vbDefaultButton3,


"MsgBoxDemo For B.Com(CA)"

The
default message box is VBOK only, which the user can click the OK button and end the message box.
The user can choose his own message box, either by specifying the value of the message box or by
giving the full name of the message box. The table below summarizes its information
You combine three different groups of built-in integer constants to specify the kind of
message box. The first number controls what kind of buttons appears. The following table summarizes
this information.

Constant Value Description

vbOK 1 OK

vbCancel 2 Cancel

vbAbort 3 Abort

vbRetry 4 Retry

vbIgnore 5 Ignore

vbYes 6 Yes

vbNo 7 No

Constant Value Description

vbOKOnly 0 Display OK button only.

vbOKCancel 1 Display OK and Cancel buttons.

vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons.

vbYesNoCancel 3 Display Yes, No, and Cancel buttons.

vbYesNo 4 Display Yes and No buttons.

vbRetryCancel 5 Display Retry and Cancel buttons.

vbCritical 16 Display Critical Message icon.

vbQuestion 32 Display Warning Query icon.

vbExclamation 48 Display Warning Message icon.

vbInformation 64 Display Information Message icon.

vbDefaultButton1 0 First button is default.

vbDefaultButton2 256 Second button is default.

vbDefaultButton3 512 Third button is default.

vbDefaultButton4 768 Fourth button is default.

vbApplicationModal 0 Application modal; the user must respond to the message box before
continuing work in the current application.

vbSystemModal 4096 System modal; all applications are suspended until the user responds to the
message box.

vbMsgBoxHelpButton 16384 Adds Help button to the message box

VbMsgBoxSetForeground 65536 Specifies the message box window as the foreground window

vbMsgBoxRight 524288 Text is right aligned

vbMsgBoxRtlReading 1048576 Specifies text should appear as right-to-left reading on Hebrew and Arabic
systems
The user can set a default button, which dining the nm time the message box default selects a
particular button. This has three values, which are shown below with their information. The next
group of numbers controls which button is the default button for the box. This is summarized in the
following table.
You can combine these options by adding the constants or values together. We can also use
some picture to be shown on the message box, some like exclamation, question marks etc.,

TEXT BOXES: Text boxes are the primary method for accepting input and displaying output
in VB. In fact, printing too many lines of test to a form will often load to run time error and, in any
case, you can’t scroll back through the form to see that may have slipped off the top.
Properties of Text Box
There are around 50 properties for text boxes. Name property is used only for the code you
write, the user never see it. We can also change height, width, left, top, with the properties. You can
also set both the backcolor and forecolor properties for a text box. This can be set using color palette
from the property window.
The enable property affects whether the text box will respond the events. If a text box is disabled the
user cannot enter text inside it. It also has visible property between True or False, with code, in order
to make a text box appear and disappear.
The text boxes are the primary method for accepting input and displaying output in visual
basic. This is the only tool that you can type in during the run time. Text boxes can display large
amount of text which the user can scroll through.

Whatever is typed on the text boxes will be accepted only as strings. Even if a
number is typed it is treated as strings only. Some functions say Val is need to convert in to number
values.
Text box uses around 50 properties now we will look out some special
properties of text boxes.
TEXT: If we want a text box to be empty when application starts, select the text property and blank
out the original settings.
Alignment : The default value is 0, which is left aligned use 1 and text is right aligned. Use of value
2 is centered.
Multiline: This property determines whether a text box can accept more than one line of
Text when the user runs the application. This allows the use of horizontal and vertical
scroll bar; to move around the area.
Scroll Bars :
This property determines whether a textbox has horizontal or vertical scroll bars.
Without scroll bars it becomes much harder for the user to move through the data contained in
the text box.
0 - default value
1— horizontal scroll bar
2 — vertical scroll bar
3 — both horizontal and vertical scroll bar.
Border Style : This sets the border styles of the text box
0 — border disappears
1 - single-width border.
Max length: This property determines the maximum number of characters the text box will accept. 0
- default of about 32000—characters
Any other setting specifies the number he give.
Password character: This property lets you limit what the text box displays. The convention is to use
asterisk (*) Once we set this, the entire textbox character Will look in asterisk.
Locked: It is used to lock the text box and no action can be performed on it as display or type
anything to it.
LABEL: Use labels to display text information that the user does not need to edit. We can still change
the text displayed in your code, by setting the caption property. Probably the most common use for
labels is to identify a text box or other control by describing its contents another common use is to
display help information. This also displays some results that are produced after run time. So the user
can’t change the values as like text box.
Labels support totally 34 possible properties, some properties are same as that of form, text
box and command button properties. When label control is created it automatically sets its default
caption as label1 and label2 and so on.
Common Properties Are
• Caption - sets the caption that is given by the user to identify other control
• Font - sets the font style of the caption
• Height, width - resize of the labels
• Left & top - creates spaces other controls
• Back color - sets the back color of label as user needs
• Fore color - sets the text color of labels
• Enabled - this case T/F to make user use of labels and disables
• Visible- uses T/ F to make labels visible in form or not.
Specific Properties of Labels
There arc five especially useful properties for labels which are listed below.

ALIGNMENT: The alignment property for the label has three possible settings
— Value 0— means the text in the label for a label is left - justified
— Value I — text inside the label will be right - justified
— Values2 this sets the value in center of labels.
Border Style, Back Style
The border style property has two types of values 0 that is default with no borders and value 1
label resembles with a border. The back style property determines whether the label is transparent or
opaque.

Auto Size : The labels can be made to grow automatically in a horizontal direction to encompass the
text you place in them. The default is False and you need to change it to True to take advantage of
these neat features.
Word Wrap : If we set the property to true, the label Will grow in the vertical direction encompass its
contents, but the horizontal size will stay the same. This helps in not breaking any words, we need to
set this property to 1 for using this option 0, 3 default.

PICTURE BOX Picture box, as its name suggest, is a box which can contain a picture. Less
obviously we can draw on a picture box in our own code. A picture box can also be a container for
other controls. So we can group them together.

Placing control on the form:


Click the picture box icon in the toolbox and place one on a form. We can resize the picture box by
manipulating the sizing handles.

Loading the picture:


There are two ways in loading the picture one is to use the property picture to browse for the
picture location. Another way is to place a picture during the run time by specifying the file name with
the full path.
You can Load bitmaps icon and meta files, bit maps must have a bmp, jpg or gif extension.
Icons have an ico extension. Metafiles have either a wmf or emf extension.
Picture box name. Picture = load picture (filename)

There are different properties events and methods used in the picture box some are shown
below.
Working with Picture Boxes:
Picture boxes have over 50 properties and respond to 19 events, and you can use any one of 22
methods for them. Some are listed below
Properties of Picture Boxes:
Insert from
The special property is auto size is true the picture box will automatically resize itself to fit the
image.
Methods of Picture Box:
This uses 22 methods some of the main methods are Cls. This erases whatever image and text
were placed in the picture box. Move method moves the picture box around at run time to make
animated effect.
Some events procedures are
Event Occurs
Click when a user clicks an object
Double click When a user double clicks an object.
Drag drop When a user performs a drag operation from
a different object, and releases (drops) the
mouse over the cunent object.
While a user is perfonning a ding opention
Drag over from a different object and the cursor is over
the current control This event is often used to
display a custom icon indicating whether or
not the current control can accept the “Drop”
action.
Got focus When the current object receives the focus.
This can happen when a user click the object
(also triggering the click event),
When someone uses the tab key to give the
object the focus, or when the focus is set to
the object in visual basic code.

Key down When a user first presses a key while the


current control has the locus.
after a user presses and then releases a key
Key press
while the current object has the focus.

When a user releases a key that is pressed


key up while the current object has the focus. The
key up event occurs after the key down event
When a form is first loaded. This event is
used to place code that performs initializing
functions needed for the form the first time it
Load
is viewed.
When the object loses the focus, triggering
the got focus event of the object that is
Lost focus receiving the focus.
When a user first presses a mouse button on
the object.
Mouse down
When a user moves the mouse over the
object. This event continues to fire as long as
Mouse move the mouse is being moved over the object.
When the user releases a mouse button that
was originally pressed over the control.

Mouse up

You might also like