Wednesday, 22 August 2018

VB controls

1) Label
Label controls to provide a descriptive caption and possibly an associated hot key for other controls, such as
TextBox, ListBox, and ComboBox, that don't expose the Caption property. In most cases, you just place a Label
control where you need it, set its Caption property to a suitable string (embedding an ampersand character in
front of the hot key you want to assign), and you're done. Caption is the default property for Label controls.
Other useful properties are BorderStyle(if you want the Label control to appear inside a 3D border) and
Alignment (if you want to align the caption to the right or center it on the control). In most cases, the alignment
depends on how the Label control relates to its companion control: for example, if the Label control is placed to
the left of its companion field, you might want to set its Alignment property to 1-Right Justify. The value 2-
Center is especially useful for stand-alone Label controls.
You can insert a literal & character in a Label control's Caption property by doubling it. For example, to see
Research & Development you have to type &Research && Development. Note that if you have multiple but
isolated &s, the one that selects the hot key is the last one and all others are ignored. This tip applies to all the
controls that expose a Caption property. (The & has no special meaning in forms' Caption properties, however.)
If the caption string is a long one, you might want to set the Label's WordWrap property to True so that it will
extend for multiple lines instead of being truncated by the right border of the control. Alternatively, you might
decide to set the AutoSize property to True and let the control automatically resize itself to accommodate longer
caption strings.
You sometimes need to modify the default value of a Label's BackStyle property. Label controls usually cover
what's already on the form's surface (other lightweight controls, output from graphic methods, and so on)
because their background is considered to be opaque. If you want to show a character string somewhere on the
form but at the same time you don't want to obscure underlying objects, set the BackStyle property to 0-
Transparent.
2) TextBox
TextBox controls offer a natural way for users to enter a value in your program. For this reason, they tend to be
the most frequently used controls in the majority of Windows applications. TextBox controls, which have a
great many properties and events, are also among the most complex intrinsic controls.
Setting properties to a TextBox
 Text can be entered into the text box by assigning the necessary string to the text property of the control
 If the user needs to display multiple lines of text in a TextBox, set the MultiLine property to True
 To customize the scroll bar combination on a TextBox, set the ScrollBars property.
 Scroll bars will always appear on the TextBox when it's MultiLine property is set to True and its
ScrollBars property is set to anything except None(0)
 If you set the MultilIne property to True, you can set the alignment using the Alignment property. The
test is left-justified by default. If the MultiLine property is et to False, then setting the Alignment property
has no effect.
Run-Time Properties of a TextBox control
The Text property is the one you'll reference most often in code, and conveniently it's the default property for
the TextBox control. Three other frequently used properties are these:
 The SelStart property sets or returns the position of the blinking caret (the insertion point where the
text you type appears). Note that the blinking cursor inside TextBox and other controls is named caret, to
distinguish it from the cursor (which is implicitly the mouse cursor). When the caret is at the beginning of the
contents of the TextBox control, SelStart returns 0; when it's at the end of the string typed by the user, SelStart
returns the value Len(Text). You can modify the SelStart property to programmatically move the caret.
 The SelLength property returns the number of characters in the portion of text that has been
highlighted by the user, or it returns 0 if there's no highlighted text. You can assign a nonzero value to this
property to programmatically select text from code. Interestingly, you can assign to this property a value
larger than the current text's length without raising a run-time error.
 The SelText property sets or returns the portion of the text that's currently selected, or it returns an
empty string if no text is highlighted. Use it to directly retrieve the highlighted text without having to query
Text, SelStart, and SelLength properties. What's even more interesting is that you can assign a new value to
this property, thus replacing the current selection with your own. If no text is currently selected, your string is
simply inserted at the current caret position.
The following Figure summarizes the common TextBox control's properties and methods.
Property/
Method Description
Properties
Enabled specifies whether user can interact with this control or not
Index Specifies the control array index
Locked If this control is set to True user can use it else if this control is set to false the control
cannot be used
MaxLength Specifies the maximum number of characters to be input. Default value is set to 0 that
means user can input any number of characters
MousePointer Using this we can set the shape of the mouse pointer when over a TextBox
Multiline By setting this property to True user can have more than one line in the TextBox
PasswordChar This is to specify mask character to be displayed in the TextBox
ScrollBars
This to set either the vertical scrollbars or horizontal scrollbars to make appear in the
TextBox. User can also set it to both vertical and horizontal. This property is used with the
Multiline property.
Text Specifies the text to be displayed in the TextBox at runtime
ToolTipIndex This is used to display what text is displayed or in the control
Visible By setting this user can make the Textbox control visible or invisible at runtime
Methods
SetFocus Transfers focus to the TextBox
3) PictureBox Control
PictureBox controls are among the most powerful and complex items in the Visual Basic Toolbox window. In a
sense, these controls are more similar to forms than to other controls. For example, PictureBox controls support
all the properties related to graphic output, including AutoRedraw, ClipControls, HasDC, FontTransparent,
CurrentX, CurrentY, and all the Draw, Fill, and Scale properties. PictureBox controls also support all graphic
methods, such as Cls, PSet, Point, Line, and Circle and conversion methods, such as ScaleX, ScaleY,
TextWidth, and TextHeight.
Loading images
Once you place a PictureBox on a form, you might want to load an image in it, which you do by setting the
Picture property in the Properties window. You can load images in many different graphic formats, including
bitmaps (BMP), device independent bitmaps (DIB), metafiles (WMF), enhanced metafiles (EMF), GIF and
JPEG compressed files, and icons (ICO and CUR). You can decide whether a control should display a border,
resetting the BorderStyle to 0-None if necessary. Another property that comes handy in this phase is AutoSize:
Set it to True and let the control automatically resize itself to fit the assigned image.
You might want to set the Align property of a PictureBox control to something other than the 0-None value. By
doing that, you attach the control to one of the four form borders and have Visual Basic automatically move and
resize the PictureBox control when the form is resized. PictureBox controls expose a Resize event, so you can
trap it if you need to move and resize its child controls too. You can do more interesting things at run time. To
begin with, you can programmatically load any image in the control using the LoadPicture function:
Picture1.Picture = LoadPicture("c:\windows\setup.bmp")
4) Frame Controls
Frame controls are similar to Label controls in that they can serve as captions for those controls that don't have
their own. Moreover, Frame controls can also (and often do) behave as containers and host other controls. In
most cases, you only need to drop a Frame control on a form and set its Caption property. If you want to create
a borderless frame, you can set its BorderStyle property to 0-None.
Controls that are contained in the Frame control are said to be child controls. Moving a control at design time
over a Frame control—or over any other container, for that matter—doesn't automatically make that control a
child of the Frame control. After you create a Frame control, you can create a child control by selecting the
child control's icon in the Toolbox and drawing a new instance inside the Frame's border. Alternatively, to make
an existing control a child of a Frame control, you must select the control, press Ctrl+X to cut it to the
Clipboard, select the Frame control, and press Ctrl+V to paste the control inside the Frame. If you don't follow
this procedure and you simply move the control over the Frame, the two controls remain completely
independent of each other, even if the other control appears in front of the Frame control.
Frame controls, like all container controls, have two interesting features. If you move a Frame control, all the
child controls go with it. If you make a container control disabled or invisible, all its child controls also become
disabled or invisible. You can exploit these features to quickly change the state of a group of related controls.
5) Command Control
Using Command Button controls is trivial. In most cases, you just draw the control on the form's surface, set its
Caption property to a suitable string (adding an & character to associate a hot key with the control if you so
choose), and you're finished, at least with user-interface issues. To make the button functional, you write code
in its Click event procedure, as in this fragment:
Private Sub Command1_Click()
' Save data, then unload the current form.
Call SaveDataToDisk
Unload Me
End Sub
You can use two other properties at design time to modify the behavior of a Command Button control. You can
set the Default property to True if it's the default push button for the form (the button that receives a click when
the user presses the Enter key—usually the OK or Save button). Similarly, you can set the Cancel property to
True if you want to associate the button with the Escape key.
The only relevant Command Button's run-time property is Value, which sets or returns the state of the control
(True if pressed, False otherwise). Value is also the default property for this type of control. In most cases, you
don't need to query this property because if you're inside a button's Click event you can be sure that the button is
being activated. The Value property is useful only for programmatically clicking a button:
This fires the button's Click event.
Command1.Value = True
Properties of a CommandButton control
 To display text on a CommandButton control, set its Caption property.
 An event can be activated by clicking on the CommandButton.
 To set the background colour of the CommandButton, select a colour in the BackColor property.
 To set the text color set the Forecolor property.
 Font for the CommandButton control can be selected using the Font property.
 To enable or disable the buttons set the Enabled property to True or False
 To make visible or invisible the buttons at run time, set the Visible property to True or False.
 Tooltips can be added to a button by setting a text to the Tooltip property of the CommandButton.
6) OptionButton
OptionButton controls are also known as radio buttons because of their shape. You always use OptionButton
controls in a group of two or more because their purpose is to offer a number of mutually exclusive choices.
Anytime you click on a button in the group, it switches to a selected state and all the other controls in the group
become unselected.
Preliminary operations for an OptionButton control are similar to those already described for CheckBox
controls. You set an OptionButton control's Caption property to a meaningful string, and if you want you can
change its Alignment property to make the control right aligned. If the control is the one in its group that's in the
selected state, you also set its Valueproperty to True. (The OptionButton's Value property is a Boolean value
because only two states are possible.) Value is the default property for this control.
In the following example, the shape control is placed in the form together with 3 Option Boxes. When the user
clicks on different option boxes, different shapes will appear. The values of the shape control are 0, 1, and 2
which will make it appear as a rectangle, a square, an oval shape, respectively.
Private Sub Option1_Click ( )
Shape1.Shape = 0
End Sub
Private Sub Option2_Click()
Shape1.Shape = 1
End Sub
Private Sub Option3_Click()
Shape1.Shape = 2
End Sub
7) Check Box Control
The CheckBox control is similar to the option button, except that a list of choices can be made using check
boxes where you cannot choose more than one selection using an OptionButton. By ticking theCheckBox the
value is set to True. This control can also be grayed when the state of the CheckBox is unavailable, but you
must manage that state through code.
When you place a CheckBox control on a form, all you have to do, usually, is set its Caption property to a
descriptive string. You might sometimes want to move the little check box to the right of its caption, which you
do by setting the Alignment property to 1-Right Justify, but in most cases the default setting is OK. If you want
to display the control in a checked state, you set its Value property to 1-Checked right in the Properties window,
and you set a grayed state with 2-Grayed.
The only important event for CheckBox controls is the Click event, which fires when either the user or the code
changes the state of the control. In many cases, you don't need to write code to handle this event. Instead, you
just query the control's Value property when your code needs to process user choices. You usually write code in
a CheckBox control's Click event when it affects the state of other controls.
Example
Private Sub Command1_Click()
If Check1.Value = 1 And Check2.Value = 0 Then
MsgBox "Apple is selected"
ElseIf Check2.Value = 1 And Check1.Value = 0 Then
MsgBox "Orange is selected"
Else
MsgBox "All are selected"
End If
End Sub
8) Combo Box
ComboBox controls present a set of choices that are displayed vertically in a column. If the number of items
exceed the value that be displayed, scroll bars will automatically appear on the control. These scroll bars can be
scrolled up and down or left to right through the list.
The following Fig lists some of the common ComboBox properties and methods.
Property/Method Description
Properties
Enabled By setting this property to True or False user can decide whether user can interact
with this control or not
Index Specifies the Control array index
List String array. Contains the strings displayed in the drop-down list. Starting array
index is 0.
ListCount Integer. Contains the number of drop-down list items
ListIndex Integer. Contains the index of the selected ComboBox item. If an item is not
selected, ListIndex is -1
Locked Boolean. Specifies whether user can type or not in the ComboBox
MousePointer Integer. Specifies the shape of the mouse pointer when over the area of the
ComboBox
NewIndex Integer. Index of the last item added to the ComboBox. If the ComboBox does not
contain any items , NewIndex is -1
Sorted Boolean. Specifies whether the ComboBox's items are sorted or not.
Style Integer. Specifies the style of the ComboBox's appearance
TabStop Boolean. Specifies whether ComboBox receives the focus or not.
Text String. Specifies the selected item in the ComboBox
ToolTipIndex String. Specifies what text is displayed as the ComboBox's tool tip
Visible Boolean. Specifies whether ComboBox is visible or not at run time
Methods
AddItem Add an item to the ComboBox
Clear Removes all items from the ComboBox
RemoveItem Removes the specified item from the ComboBox
SetFocus Transfers focus to the ComboBox
9) List Box
The function of the List Box is to present a list of items where the user can click and select the items from the
list. In order to add items to the list, we can use the AddItem method. For example, if you wish to add a
number of items to list box 1, you can key in the following statements
Example
Private Sub Form_Load ( )
List1.AddItem “Lesson1”
List1.AddItem “Lesson2”
List1.AddItem “Lesson3”
List1.AddItem “Lesson4”
End Sub
The items in the list box can be identified by the ListIndex property, the value of the ListIndex for the first item
is 0, the second item has a ListIndex 1, and the second item has a ListIndex 2 and so on
The following common ListBox properties and methods are same as Combobox Properties & methods..
10) Drive List Box
The Drive ListBox is for displaying a list of drives available in your computer. When you place this control into
the form and run the program, you will be able to select different drives from your computer.
Using these controls, user can traverse the host computer's file system; locate any folder or files on any hard
disk, even on network drives. The files are controls are independent of one another, and each can exist on it's
own, but they are rarely used separately. In a nutshell, the DriveListBox control is a combo box-like control
that's automatically filled with your drive's letters and volume labels.
11) Directory List Box
The Directory List Box is for displaying the list of directories or folders in a selected drive. When you place this
control into the form and run the program, you will be able to select different directories from a selected drive
in your computer. The DirListBox is a special list box that displays a directory tree. When the user selects a
path in the DirListBox control, the FileListBox control is filled with the list of files in that directory.
12) FileList Box
The File List Box is for displaying the list of files in a selected directory or folder. When you place this control
into the form and run the program, you will be able to shown the list of files in a selected directory.
The FileListBox control, on the other hand, exposes one property that you can set at design time—the Pattern
property. This property indicates which files are to be shown in the list area: Its default value is *.* (all files),
but you can enter whatever specification you need, and you can also enter multiple specifications using the
semicolon as a separator.
13) Horizontal Scrollbar
The Scroll Bar is a commonly used control, which enables the user to select a value by positioning it at the
desired location. It represents a set of values. The Min and Max property represents the minimum and
maximum value. The value property of the Scroll Bar represents its current value that may be any integer
between minimum and maximum values assigned.
The Horizontal Scrollbar controls are perfectly identical, apart from their different orientation. After you place
an instance of such a control on a form, you have to worry about only a few properties: Min and Max represent
the valid range of values, SmallChange is the variation in value you get when clicking on the scroll bar's arrows,
and LargeChange is the variation you get when you click on either side of the scroll bar indicator. The default
initial value for those two properties is 1, but you'll probably have to change LargeChange to a higher value. For
example, if you have a scroll bar that lets you browse a portion of text, SmallChange should be 1 (you scroll
one line at a time) and LargeChange should be set to match the number of visible text lines in the window.
14) Vertical Scrollbar
The Vertical Scroll Bar is a commonly used control, which enables the user to select a value by positioning it at
the desired location. It represents a set of values. The Min and Max property represents the minimum and
maximum value. The value property of the Scroll Bar represents its current value that may be any integer
between minimum and maximum values assigned.
The Vertical Scrollbar controls are perfectly identical, apart from their different orientation. After you place an
instance of such a control on a form, you have to worry about only a few properties: Min and Max represent the
valid range of values, SmallChange is the variation in value you get when clicking on the scroll bar's arrows,
and LargeChange is the variation you get when you click on either side of the scroll bar indicator. When a
scrollbar control has the focus, you can move the indicator using the Left, Right, Up, Down, PgUp, PgDn,
Home, and End keys.
15) Timer
A Timer control is invisible at run time, and its purpose is to send a periodic pulse to the current application.
You can trap this pulse by writing code in the Timer's Timer event procedure and take advantage of it to execute
a task in the background or to monitor a user's actions. This control exposes only two meaningful properties:
Interval and Enabled. Interval stands for the number of milliseconds between subsequent pulses (Timer events),
while Enabled lets you activate or deactivate events. When you place the Timer control on a form, its Interval is
0, which means no events.
16) Shape Control
In a sense, the Shape control is an extension of the Line control. It can display six basic shapes: Rectangle,
Square, Oval, Circle, Rounded Rectangle, and Rounded Square. It supports all the Line control's properties and
a few more: BorderStyle (0-Transparent, 1-Solid), FillColor, and FillStyle (the same as a form's properties with
the same names). The same performance considerations I pointed out for the Line control apply to the Shape
control.
17) Line Control
The Line control is a decorative control whose only purpose is let you draw one or more straight lines at design
time, instead of displaying them using a Line graphical method at run time. This control exposes a few
properties whose meaning should sound familiar to you by now: BorderColor (the color of the line),
BorderStyle (the same as a form's DrawStyle property), BorderWidth (the same as a form's DrawWidth
property), and DrawMode. While the Line control is handy, remember that using a Line method at run time is
usually better in terms of performance.
18) Image Control
Image controls are far less complex than PictureBox controls. They don't support graphical methods or the
AutoRedraw and the ClipControls properties, and they can't work as containers, just to hint at their biggest
limitations. Nevertheless, you should always strive to use Image controls instead of PictureBox controls because
they load faster and consume less memory and system resources. Remember that Image controls are
windowless objects that are actually managed by Visual Basic without creating a Windows object. Image
controls can load bitmaps and JPEG and GIF images.
When you're working with an Image control, you typically load a bitmap into its Picture property either at
design time or at run time using the LoadPicture function. Image controls don't expose the AutoSize property
because by default they resize to display the contained image (as it happens with PictureBox controls set at
AutoSize = True). On the other hand, Image controls support a Stretch property that, if True, resizes the image
(distorting it if necessary) to fit the control. In fact, you can zoom in to or reduce an image by loading it in an
Image control and then setting its Stretch property to True to change its width and height.

No comments:

Post a Comment