Sunteți pe pagina 1din 10

UNIT 4 VISUAL BASIC CONTROLS

Structure

4.0 Introduction
4.1 Objectives
4.2 What iq a Control?
4.3 What is a Custom Control?
4.4 Controls in a Fnrrn
. 1'!cf11rc Pnk
3.4.2 Lahei Controls
4.4.3 'Text Control
4.4.4 Command Button
4.4.5 Shape
4.5 Summary

4.0 INTRODUCTION

In the previous three chapters. you have been exposed to the basics of Visual Basic. You are now
familiar with various type< nf variahles and functions. In this chapter, you will learn about the
Visual Raqic controls and thc custom controls, which can be used in the forms. This controls make
the environment very user friendl!.

4.1 OBJECTIVES

After completion of this unit. you will be able to:

Discuss various controls.

Add the controls on the form and can change their properties.

4.2 WHAT IS A CONTROL?

A control is a speiial !t pc of oh.ject that one draws on a Form to enable user interaction with an
application. In Visual Basic. most of the objects appear inside forms. All objects that appear inside
a form are callcd controls. Vcni15and menu items are all controls. One can add menu controls using
the Menu Editor. The tool< which are also controls appear in the Toolbox and can be placed on a
form by douhle-clicking or by click-drag operation onto a form.

('ontrols muit lla\ c name\. Controls ;lccepf user input or display output. Controls have properties
that deline aspect<of their appearance, such as positiqn, size, and color, and aspects of their
behaviour, such as how they respond to user input.
Controls can respond to events initiated by the user. They also respond to the events triggered by Visual Basic
Controls
the system. For example, if one wants that whenever the command button is selected it should
perform the action desired and should display the result. For this you can write code in a Command
Button control's Click event procedure that would load a file or perform a calculation and then
display the result.

In addition to properties and events. one can use methods to manipulate controls from within the

I
code. For example. one can use the Move method with some controls to change their location and
size. Each control has its own set of properties. events. and methods.

Graphical controls include the Image. Label. Line. and Shape controls. A &aphical control uses
fewer system resources and has different drawinp and display characteristics than other controls.

4.3 WHAT IS A CUSTOM CONTROL?

The custom control is a program that someone has writtell which can hc included in the Visual
Basic program. The two types of custom controls that Visual Basic can use are VBX (Visual Basic
custom extension controls) and OCX (OLE Custom extension controls). A file with a .VBX or
.OCX extension or an insertahle object that. when added to a pro-ject using the custom controls
dialog box. extends the Toolbox.

The custom controls are prosrams. They are likc!! to have thc hu;s in them. This might create a
problem since the source code of these controls is not available with you and without that it is not
possihle to remove the bug.

To load the custom controls

I Jsethe Custom Controls dialog bos to load custom controls and insertable objects to your prosect's
Toolbox.

To open the Custom Controls dialog hox. choose C,'uctom Controls from the TooLy menu. This
operation can also he performed by pressins CtrlLT.

I'hc dialog hos displa? \ :IT av;li:ai-ic custom coiltrois an6 I?~scrta'nic
oijects. You wil! also find the
Components of
Windows
Programming
& Visual Basic

box appearing on the left of the list of controls. If the insertable objects or controls are not
appearing completely, you can click at the respective boxes and then the complete list shall appear.

One can select either or both of the following options.

~AsertableObjects option displays insertable objects, such as a Microsoft Excel Chart.

Controls option displays controls with .OCX and .VBX filename extensions.

Selected Items Only option displays only those items in the Available Controls which you have
been selected to include in the project.

If one wants to load a custom control each time Visual Basic is loaded, follow the following
procedure.

TOadd a control or insertable object to the Toolbox, click on the check box next to its name.
You will find 'X' mark appearing in the box.

To remove a control or insertable object from the project, click the check box next to its name.
Clicking this time shall deselect the control from the list available and 'X' mark shall disappear
from the box.

When you are finished making selections, Click on OK button to update your Toolbox.
Visual Basic
4.4 CONTROLS IN A FORM Controls

4.4.1 Picture Box

To display a picture on the form, you have to have a Picture Box, A PictureBox control can display
a graphic from a bitmap, icon, or metafile. It clips the graphic if the control is not large enough to
display the entire image.
I

I To use the PictureBox control, Click on this Picture Box button in the Toolbox and draw it on the
form.

Click on the Picture property button of the PictureBox to open picture dialog box.

Click here

'You will be displayed the files in the Load Picture box which can be loaded. Select a picture file of
your choice and click on the Open button.
Components of
Windows
Programming
& Visual Basic

Now you ca

A Label control is a graphical control that one can use to display text that a user can not change
directly.

Select Label control from the Toolbox and draw it in the form.
Visunl Rnsic
Controls

By default, the label name is given as Label 1 (label one), but one can change the name as desired
using the Caption property.

1 Labell Label

You can also change the font and size of the label. Click on the Font property button to display Font
dialog box.

Click here

Select the Font and size from the rcspective list boxcs and click on the OK button.
Windm
4.4.3 Text Control
Programming
& Visual Basic

A TextBox control, sometimes called an edit control, displays information entered at design time,
entered by the user, or assigned to the control in code at run time.

Select TextBox control from the Toolbox and draw it on the form.

To display multiple lines in a TextBox control, the MultiLine property of the text box should be
changed to True.

If a multiple-line TextBox does not have a horizontal scroll bar, text wraps automatically even
when the TextBox is resized. To customize the scroll bar, click on the ScrolBar option of the
TextBox and change the property.

You can also change the font and size of the text from the font dialog box. Click on the Font
property button to open font dialog box.

Click here

Select the Font and size from the respective list boxes and click on the OK button.
VIsuPl Basic
Controls

4.4.4 Command Button

Use a CommandButton control to begin, interrupt, or end a process. When chosen, a


CommandButton appears pushed in and so is sometimes called a push button.

Select CommandButton from the Toolbox and draw it on the form.

I By default its caption is Commandl, but you can change it to EXIT in the Caption property.

And now you can write code for this button to close the application. Double click on the button to
display the code window and write End command.
Compocpvts of
Windsws
1'1u g an~ming
~
& Visual Basic
Visual Basic
Check Your Progress Controls

On your computer, start the visual basic software. Once the Visual Basic is loaded, perform the
following operations:

I. Place a Picture control on the form. Change its location.

2. Load any picture on Picture control. Set its properties as per your choice.
-,

3 . Place one or two Label5 and Text boxes on the form.

4. Change label caption.

i
5. Change label size.

6. Place a command button on the form.

7. Write code, for the command button - whenever click on it, Proiect must be close.

4.5 SUMMARY

A ~ontrolis an objzct that enables users to interact with the application. These are the building
I
blocks of an application. Each control has a unique set of properties, which determine its
appearance and behavior.

A control has a name property that distinguishes it from other objects in a project. There are other
properties as well which can be changed.

The custom control is a program that someone has written which can be included in the Visual
Basic program. The two types of custom controls that Visual Basic can use are VBX (Visual Basic
custom extension controls) and OCX (OLE Custom extension controls). A custom control is a file
with a .VBX or .OCX filename extension or an insertable object that can be added to a project using
the custom controls dialog box to extend the 'Toolbox. The desired selection can be made by
clicking the box to the left of the list, which shall mark 'X' in the box. The deselection shall remove
the 'X' mark.

S-ar putea să vă placă și