VISUAL BASIC Controls 1
Controls These are the objects which can be pasted onto a form which display text, through picture boxes for graphic images, to buttons, check boxes, lists and other menus. Each control can have code attached to it. 2
ToolBox Tool Box contains the standard controls. You can add more controls by from components You can create your own controls ActiveX controls customised to specific purposes 3
Pointer Label control Frame control Check box Combo box Horizontal scroll bar Timer Control Directory list box Shape Object Image object OLE control Picture Box Text box Command Button Option Button List box Vertical scroll bar Drive list File list box Line object Data control 4
Visual Basic Controls Some of the common terminology associated with control Control as a object Property Method Event Procedure Program Statement Variable 5
Property With each object, various properties are associated which describe its characteristics. The properties can be set at design time by using property window or at run time through program statements Eg: Command1.Caption = Hello Few common properties are: Name, Caption, Appearance, Enable (True / False), Visible ( True / false) etc.. 6
Program Statement It is a keyword in the code that does the work of a program Eg: Unload formissue Call DISABLE ( Call procedure Disable) Form2.Show Setdb = OpenDatabase(C:inventory.mdb) Data1.UpdateRecord etc. 7
Variable It is a special container that holds data temporarily in a program Numbers, Charatcter strings, property values etc can be stored in variables Eg : Dim db As Database Dim strdiv As String Dim rs As Recordset Country = India etc. 8
Method It is a special statement that performs an action or a service for a particular object in a program Syntax : Object.Method Value Eg : List1.AddItem Printer ShowOpen SowPrint etc. 9
Form Events Whenever an event is triggered ( click, drag, resize, double click, key press etc.) a message is generated that describes your action.this message is sent to appropriate control, which in turn generates appropriate action based on pre-defined method. The event message can be customised the way action needs to be taken by writing your own code 10
Event Procedure It is a block of code that is executed when an object is manipulated in a program. Eg: When an event takes place, block of program code associated with it gets executed. Few common events are : Click, Dbclick, MouseDown, MouseUp, MouseMove etc. 11
Form Methods It is a command that allows you to tell an object what to do eg: Show hide, Unload, Print An example of programming construct: If Lable1.Caption = xxx Then hide form1 Object Property Assigned VB method Object name name Operator Function 12
Nomenclature convention of the objects For forms ( three nomenclatures are associated with it) 1. Name of the object form 2. Name of the caption of the form 3 Form file name for saving it as a part of project To avoid any confusion later, one should follow a convention of nomenclature, and should strictly follow throughout the project It is suggested to follow a convention:illustrated as follows 1 name the form object as -> frmsample 2. Name of the caption as -> Sample 3. Name of the file for saving it: -> Sample. 13
Nomenclature convention of For other objects the objects It is suggested to follow a convention:illustrated as follows 1 Name of an object frmsample, txtname, mnumain, cmdok, cmdadditem, datcourses etc. 2. Caption of the object The way, you want it to appear on the screen etc.. 14
Pointer The pointer is used for selecting objects which is to placed on the form. 15
Picture box A Picture box holds pictures created with the paintbrush or similar art packages. These must be in acceptable format like.bmp,.wmf,.ico etc..bmp Bitmap.WMF Windows MetaFile.ICO Icons.GIF Graphic Interchange Format.JPG Joint Photographic Expert Group. DIB Device Independent Bitmap.EMF Enhanced MetaFile 16
Image Control Image control is designed specifically for displaying images and not for creating new images or manipulating 17
Labels and Text Boxes Labels and Text boxes both holds text, but only Text Box is capable of receiving input from the user. Command Button Command buttons are one method of selection. They are typically used Where there are limited number of options - the choice may be be OK or Cancel, Start or Quit. 18
USE OF TEXTBOX & COMMAND BUTTON 1. Click the textbox control in the toolbox. 2. Move the mouse pointer to the form. Draw the Textbox of your need. 3. Click the command button control to the form 4. Draw the command button of any size. 5. Use the Properties set window & set the Properties as Control Property Setting Text1 Text (empty) Command1 Caption OK 6. Double click the OK command button. Type the following Text1.Text = Starting of Visual Basic 7. Now Run the program 19
Frame Control A Frame simply holds things together. If you place a set of controls with in a frame, they are all moved together as the frame is moved. Note:- that once a control has been placed in a frame, it cannot be moved out onto the form, and vice versa. 20
Check Boxes Check boxes act as toggle switches, turning options on or off. 21
Option buttons Option buttons are normally used to select one from a set of mutually exclusive options. At any one time there can be only one set on a form, unless they are enclosed in a frame. 22
List box List boxes display list of items, so that user can see what is available and select one. If the list is too long to fit in box, vertical scroll bar will be added at run time. Items can only be added to the list at the run- time, not at design time. 23
Use of List Box 1. Drag the List Box on the Form 2. Double Click the List Box and type the following Private Sub Form_Load() List1.AddItem "White" List1.AddItem "Black" List1.AddItem "Yellow" List1.AddItem "Green" List1.AddItem "Red" End Sub 3. Run 24
Combo box A Combo box combine a dropdown list with a slot in which user can enter their own data when the program is running 25
Use of Combo box 1. Drag the List Box on the Form 2. Double Click the List Box and type the following Private Sub Form_Load() Combo1.AddItem "one" Combo1.AddItem "two" Combo1.AddItem "three" Combo1.AddItem "four" Combo1.AddItem "five" Combo1.AddItem "six End Sub 3. Run 26
Horizontal and Vertical scroll bars The Horizontal and Vertical scroll bars are used on forms to give a very flexible means of setting values. When the Program is running, the position of the slider determines the value returned by the scroll bar. 27
Timer The Timer is unusual in that it is to be fixed size and is invisible once the program is running. Its purpose is to control actions the must take place at or after set intervals. EXAMPLE 28
Private Sub Command1_Click() If Text1.Text = "secret" Then Timer1.Enabled = False MsgBox ("Welcome To the System") Else MsgBox ("Sorry, Friend, I don't know you.") End If End Sub Private Sub Timer1_Timer() MsgBox ("Sorry, Your time is up.") End End Sub 29
Drive List box, Directory List box and File List box The Drive List box,directory List box and File List box can be used together to give the standard Windows file management facilities. When the program runs they will initially display the current drive and directory. These can be changed by clicking and highlighting in the usual way. Three boxes together can produce a highly efficient file selection routine. 30
USE OF DRIVELISTBOX, DIRECTORY LISTBOX, FILE LISTBOX & IMAGE CONTROLS 1. Click thedrivelistbox in the toolbox. 2. Move the mouse pointer to the upper-left corner of the form and then draw a drivelistbox. 3. Similarly draw the Directorylistbox and filelistbox. 4. Add the image box control to the right side of your form. 5. Double click the drivelistbox and type the following Dire1.Path = Drive1.Drive Close the Code window 6. Double click the directorylistbox and type the following File1.Path = Dir1.Path Close the Code window 7. Double click the Filelistbox and add the following code to the File1_Click event procdure. SelectFile = File1.Path & \ & File1.filename Image1.Picture = LoadPicture(SelectedFile) 8. Now Run the program. 31
The Shapes (either a circle, oval, square or rectangle) and Line are purely for decorative purpose 32
Data Control The Data control allows you to read records in from a database 33
OLE OLE stands for Object Linking and Embedding. This control allows you to make a two way link with an object in another Windows application. 34