Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Visual Basic Controls

? Visual Basic Controls


Controls are the elements of the User Interface with which the user can interact and control the application. Form is the first and main element of the user interface which acts as container for other elements. The basic controls are displayed as icons on the toolbox which is a part of the interface during the design time. The default icons with their names is given as below: Pointer Label Frame Check Combo Horizontal Timer Directory List Box Shape Image Control OLE Picture Box Text Box Command Radio Button List Box Vertical Drive List Box File List Box Line Data (DAO)

Training Division, NIC, New Delhi C 1

Visual Basic Controls

? Standard Visual Basic Controls Control Control Name Button CheckBox Name Prefix chk Function Example in

Displays or enables input of Assignment 2 a two-part choice, such as VB Controls Yes/No or True/False. Enables the user to select an entry from a list or enter a new value. Enables the user to initiate a program action. Can include an icon, caption, and ToolTips. Assignment 3 VB Controls Assignment 1 in VB Controls

ComboBox

cbo

in

CommandButton

cmd

Data Control Dir ListBox

dat dir

Provides a link to database files. Displays and enables a user Assignment 1 in to choose from available VB Programming subdirectories on a drive. Displays and enables a user to choose from available disk drives on the computer. Displays and enables a user to choose from available files in a directory. Serves as a container for other controls. Provides a method for grouping controls. (To group controls in a frame, select the frame with a single-click first, then draw a control in it.) Produces a numerical value based on the scroll bar's horizontal position. Displays a graphic image. Similar in appearance to the picture control but with different functionality. Displays text that the user Assignment 1 in VB Programming

DriveListBox

drv

FileListBox

fil

Assignment 1 in VB Programming Assignment 2 VB Controls in

Frame

fra

HscrollBar (Horizontal Scroll bar) Image

hsb

img

Assignment 1 in VB Programming

Label (lbl)

lbl

Assignment 4

in

Training Division, NIC, New Delhi C 2

Visual Basic Controls cannot directly modify. VB Controls Displays lines on the form. Displays a list of items Assignment 3 from which the user can VB Controls select one or more entries. Provides you with a way to link to OLE servers. Displays or enables a choice among multiple items. (Also known as a radio button.) Displays a graphic image. It can also serve as a container for other controls Displays geometric shapes on the form. Displays text that the user can edit. Assignment 4 VB Controls Assignment 2 VB Controls in

Line ListBox

lin lst

OLE Container OptionButton

ole opt

in in

PictureBox (Pic)

Pic

Shape TextBox Timer

shp txt tmr

Assignment 1 VB Controls

in in

VscrollBar (Vertical Scroll bar)

vsb

Provides a means for an Assignment 5 action to be taken after VB Controls passage of a certain amount of time. Same as above but vertical. Note the scrollbars behave like standard Windows scrollbars.

? Placing a control on a form


To place a control into use, select the control from the toolbox. The mouse pointer should change to a placement icon when placed over a form. Select the location to place the control and either click on the form, or click on a location and drag the mouse to adjust the controls visible size. Note- Not all controls are re-sizable.

Training Division, NIC, New Delhi C 3

Visual Basic Controls

Figure 2

? Changing control properties


Each control has a set of values associated with it known as its properties. These properties describe how the control will appear on a form. For instance, a textbox control will have a property called text which describes the contents of the textbox. To change the properties for a given control, click on the control to select it. If the properties window is visible, it will update to list the properties associated with the selected control and the current value of each. If the properties window is not visible, select View->Properties from the menu. In the properties window, select the property that you wish to change and edit the current value directly in the window. Note: Some controls have properties that will not appear in the properties window. These are called run-time properties and can only be changed through code.

Training Division, NIC, New Delhi C 4

Visual Basic Controls

Figure 3

? Setting control properties from code


Just as you changed control properties from the properties window, you can modify them through code. Locate a position in your code where you want to modify a controls properties. Insert a line of code with the following syntax: control_name.propertyname = new_value Where control_name is the name of your control, property_name is the name of the property that you wish to modify, and where new_value is the value you wish to set. Note- Remember to use the period (.) to separate the control from the property. example: Text1.Text = "Hello World!"

? Retrieving a property value


The process of retrieving a controls property value in code is much like setting the value in code. example: variable = Text1.TextThis example retrieves the value of the text property from the control Text1 and places the value into the variable

Training Division, NIC, New Delhi C 5

Visual Basic Controls

? Setting the tab order for controls


When your program runs, the user can navigate through each control using the Tab key. You can set the tab order for each control capable of receiving focus so as to create a structured path for the user to follow. To set a controls position in the tab order, change the controls tabindex property to an integer number designating its placement in the order. The Windows tab order runs from 0 (first) to n (last). Tip: Start with the last control on your form and set its tabindex property to 0. Click on the next to last control. The properties window should now be waiting for the tabindex of that control. Set it to 0. Each time you assign a control a tabindex of 0 (or any number) Visual Basic automatically increments (renumbers) those controls with an assigned tabindex greater than or equal to the index entered. So the last control would be renumbered to tabindex 1

? Default property Of A Control


Every control has a default property, which is usually the most used propert y of that control. For instance, the default property of the label control is the .caption property, for the text box it is the .text property, and for the image control it is the .picture property. This means that you don't have to type out the full control name and property for each control. The following pair lines of code have identical effects. textbox.text = "text" textbox = "text" label.caption = "caption" label = "caption" image.picture = LoadPicture(picture.bmp) image = LoadPicture(picture.bmp)

? Control Array
Suppose you have a form with 100 command buttons and they all accomplish pretty much the same task -- they print their caption to a label control. Now what would be better; to draw 100 command buttons and name them btnPrint1 through btnPrint100, or should you find a better way to do it? In this scenario you would undoubtedly use a control array. A control array is exactly what the name implies -- an array of controls. So instead of 100 different names, you would have btnPrint(1) through btnPrint(100).

Training Division, NIC, New Delhi C 6

Visual Basic Controls

? Setting up a Control Array


(i) In a control array, there is one control which the other controls take after when they are created. Each control in a control array also has an index so we can determine which one is to be used. You assign the index of each control by giving the control a value in it's .index property. If a control has a value in it's .index property, then it is assumed by the program to belong to a control array. Every control in the control array also has the same name, and they are distinguished as different contro ls through their index property. Let's set up a program with 5 command buttons that are part of a control array. When you click on one of the 5 buttons a notice will be displayed in a label indicating which button has been clicked. Object button button button button button label Property & its Values .name=btnControl .name=btnControl .name=btnControl .name=btnControl .name=btnControl .name=lblNotice .index=0 .index=1 .index=2 .index=3 .index=4 .caption="Button &1" .caption="Button &2" .caption="Button &3" .caption="Button &4" .caption="Button &5" .caption=""

(ii)

Notice that the indexes start from 0 and go to 4, but the captions indicate the button's number is one greater than it's index. This is taken into account when we write code for the controls. (iii) All the controls in a control array share the same code, and each event of a control in a control array is passed the control's index. So this index argument can be used in the code, as is shown below: Sub btnControl_Click (Index As Integer) lblNotice = "You selected button " & Str$(Index + 1) End Sub

Note: We're just setting the caption of lblNotice to indicate which button we pushed. We add one to Index because the first index is 0, and the caption of the first button indicates that it is number 1. So we take this into account by adding one to the index.

Training Division, NIC, New Delhi C 7

You might also like