Microsoft Visual Basic 6 (Intrinsic Control)

15

Click here to load reader

description

Transcript of Microsoft Visual Basic 6 (Intrinsic Control)

Page 1: Microsoft Visual Basic 6 (Intrinsic Control)

Visual Basic Intrinsic ControlMicrosoft Visual Basic Intrinsic Controls

The Control Properties

Before writing an event procedure for the control to response to a user's input, you have to set certain properties for the control to determine its appearance and how it will work with the event procedure. You can set the properties of the controls in the properties window or at runtime.Common Properties DescriptionAlign/Alignment Returns or sets a value that determines whether an object is

displayed in any size anywhere on a form or whether it's displayed at the top, bottom, left, or right of the form and is automatically sized to fit the form's width.

AppearanceReturns or sets a value that determines the appearance of the object.

BorderStyle Sets the border style for an object.Name The name is used in code to identify an object. AutoSize A control is automatically resized to display its entire

contents.BackColor/ForeColor Set the background or foreground color of objectCaption/Text Display text in or on an object.Enable/Visual An object is non respone or hidden from user action.

(true/False)MouseIcone Set a custom mouse icon.MousePointer Set the mouse pointer from the list.Lock Determines whether a control can be edited.Width/Height/Top/Left Set the width or height of object or set the distance from top

or left.ToolTipText The text display when mouse is paused over the control.TabIndex Set the tab order of an object within its parent form.Font Set the font size and style of object text.

Common Events DescriptionClick

Occurs when the user click a mouse button on object.DblClick Occurs when the user double click the mouse button on

object.KeyUp/KeyDown/ KeyPress

Occurs when the user press any key through keyboard on object.Keydown and KeyUp event to capture the function keys.

MouseDown/ MouseUp/MouseMove

Occurs when the user press a mouse button on object.

GotFocus Occurs when an object receives the focus, either by user action, such as tabbing to or clicking the object, or by changing the focus in code using the SetFocus method.

LostFocus Occurs when an object loses the focus, either by user action, such as tabbing to or clicking another object

Page 2: Microsoft Visual Basic 6 (Intrinsic Control)

Visual Basic Intrinsic Control

Example of KeyPress Event

Syntax

Private Sub Form_KeyPress(keyascii As Integer)

End Sub

The KeyPress event syntax has these parts:

Part Descriptionkeyascii An integer that returns a standard numeric ANSI keycode. Keyascii

is passed by reference; changing it sends a different character to the object. Changing keyascii to 0 cancels the keystroke so the object receives no character.

Example-01Private Sub Text1_KeyPress(KeyAscii As Integer) MsgBox KeyAsciiEnd SubExample-02Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii >= 48 And KeyAscii < 57 Then MsgBox "You pressed Numeric Key. " + Chr(KeyAscii) KeyAscii = 0 ElseIf KeyAscii >= 97 And KeyAscii <= 122 Then MsgBox "You pressed Small character Key. " + Chr(KeyAscii) KeyAscii = 0 ElseIf KeyAscii >= 65 And KeyAscii <= 90 Then MsgBox "You pressed capital character Key. " + Chr(KeyAscii) KeyAscii = 0 Else MsgBox "Un-Know character." & KeyAscii KeyAscii = 0 End IfEnd Sub

Example of KeyDown/KeyUp Event

Syntax

Private Sub Form_KeyDown(keycode As Integer, shift As Integer)

End Sub

The KeyDown and KeyUp event syntaxes have these parts:

Part Descriptionkeycode A key code, such as vbKeyF1 (the F1 key) or vbKeyHome (the

HOME key). To specify key codes.shift Capture the SHIFT, CTRL, and ALT keys at the time of the event.

SHIFT key (vbShiftMask), CTRL key (vbCtrlMask), ALT key (vbAltMask).

Example-01Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer) If (Shift And vbShiftMask) Then MsgBox "You pressed Shift Key." ElseIf (Shift And vbAltMask) Then MsgBox "You pressed Alt Key." ElseIf (Shift And vbCtrlMask) Then MsgBox "You pressed Control Key." ElseIf KeyCode = vbKeyF2 Then

Page 3: Microsoft Visual Basic 6 (Intrinsic Control)

Visual Basic Intrinsic Control MsgBox "You pressed F2 key." End IfEnd Sub

Example of MouseMove/Up/Down Event

Private Sub Form_MouseMove(button As Integer, shift As Integer, x As Single, y As Single)

End Sub

The Mouse event syntax has these parts:

Part Descriptionbutton Indicating which buttons are pressed.

(vbLeftButton,vbRightButton,vbMiddleButton)x, y A number that specifies the current location of the mouse pointer.

The x and y values are always expressed in terms of the coordinate system set by the ScaleHeight, ScaleWidth, ScaleLeft, and ScaleTop properties of the object.

Example-01Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = vbRightButton Then MsgBox "You press mouse right button." End IfEnd SubExample-02Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Print "Mouse x position:" & X & "Mouse y position:" & YEnd Sub

Page 4: Microsoft Visual Basic 6 (Intrinsic Control)

Visual Basic Intrinsic Control

The Form

The Form is the first object you see when you Open the application. It is the window into which all the controls will appear, where you will input data and see results.Properties DescriptionControlBox

Returns or sets a value indicating whether a Control-menu box is displayed on a form at rum time.

KeyPreview Returns or sets a value that determines whether keyboard events for forms are invoked before keyboard events for controls.

MaxButton/MinButton

Determins whether a form has a maximize/minimize button.

MDIChlid Display a form as an MDI child formMoveable Set a value that form can be moved or not.Picture Set a graphic to be display in a control.StartupPosition Set a value specifying the position of a form when it first

appears.WindowState Set the visual state of a form at run time.

Events DescriptionActivate

occurs when an object becomes the active window.Deactivate occurs when an object is no longer the active window.Initialize Occurs when an application creates an instance of a FormLoad Occurs when a form is loaded.Unload Occurs when a form is about to be removed from the screen.

When that form is reloaded, the contents of all its controls are reinitialized. This event is triggered by a user closing the form using the Close command on the Control menu or an Unload/End statement.

QueryUnload Occurs before a form or application closes.Terminate Occurs when all references to an instance of a Form are

removed from memory.Resize

Occurs when the window state of an object changes. (For example, a form is maximized, minimized, or restored.)

Example of QueryUnload Event

Syntax

Private Sub Form_QueryUnload(cancel As Integer, unloadmode As Integer)

End sub

The QueryUnload event syntax has these parts:

Part Descriptioncancel An integer. Setting this argument to any value other than 0 stops the

QueryUnload event in all loaded forms and stops the form and application from closing.

unloadmode A value or constant indicating the cause of the QueryUnload event, as described in Return Values.

Page 5: Microsoft Visual Basic 6 (Intrinsic Control)

Visual Basic Intrinsic Control

Return Values

The unloadmode argument returns the following values:

Constant Value DescriptionvbFormControlMenu

0 The user chose the Close command from the Control menu on the form.

vbFormCode 1 The Unload statement is invoked from code.vbAppWindows 2 The current Microsoft Windows operating

environment session is ending.vbAppTaskManager

3 The Microsoft Windows Task Manager is closing the application.

vbFormMDIForm 4 An MDI child form is closing because the MDI form is closing.

vbFormOwner 5 A form is closing because its owner is closing.Example-01Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) If UnloadMode = 0 Then Cancel = 0 Else Cancel = 1 End IfEnd Sub

The Label Control

This is probably the first control you will master. It is used to display static text, titles and screen output from operations. The important properties to remember:Properties DescriptionUseMnemonic Returns or sets a value that specifies whether an ampersand (&)

included in the text of the Caption property of the Label control defines an access key.

The Textbox Control

Properties DescriptionMaxLength

Set the maximum number of characters that can be entered in a textbox.

MultiLine Set a value that determines whether a control can accept multiple lines of text.

PasswordChar sets a value indicating whether the characters typed by a user or placeholder characters are displayed in a TextBox control

ScrollBars Display a scroll bar vertical or horizontal.CausesValidation

Returns or sets a value that determines whether the Validate event will occur on a second control from which the second control's focus is being shifted.

Page 6: Microsoft Visual Basic 6 (Intrinsic Control)

Visual Basic Intrinsic Control

Events DescriptionChange

Indicates the contents of a control have changed.Validate Occurs before the focus shifts to a (second) control that has its

CausesValidation property set to True.

Methods DescriptionSelText

Return the selected text.SelStart Set the position of first character.SelLength Return or set the length of the selected text.

Example-01

Private Sub Text1_GotFocus() Text1.SelStart = 0 Text1.SelLength = Len(Text1.Text)End Sub

Frame Control

A Frame control provides an identifiable grouping for controls. You can also use a Frame to subdivide a form functionally

CommandButton Control

The command button is one of the most important controls as it is used to execute commands.Properties DescriptionCancel

Set commandbutton is the cancel button on a form.(To invoke press ESC key)

DisablePicture

Set a graphic to be display when the button is disabled.

DownPicture Set a graphic to be display when the button is down position.Style Set the appearance of the control.

PictureBox Control

A PictureBox control can display a graphic from a bitmap, icon, or metafile.Properties DescriptionAutoSize

Returns or sets a value that determines whether a control is automatically resized to display its entire contents.

Picture Sets a graphic to be displayed in a control.

The Image Box Control

The Image Box is another control that handles images and pictures. It functions almost identically to the picture box. However, there is one major difference, the image in an Image Box is stretchable, which means it can be resized. This feature is not available in the Picture Box.Properties DescriptionPicture Sets a graphic to be displayed in a control.Stretch Graphic resizes to fit the size of an image control.

Page 7: Microsoft Visual Basic 6 (Intrinsic Control)

Visual Basic Intrinsic Control

The List Box Control

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.Properties DescriptionItemData Sets a specific number for each item in a combobox or listbox control.List Add or Retrive item in combobox or listbox control.MultiSelect Set a value that user can make multiple selections in a control.Sorted The control are automatically sorted alphabetically.Style Set the style of simple Listbox or checkboxes listbox.

Events DescriptionItemCheck Occurs when a ListBox control Style property is set to 1 (checkboxes)

and an item’s checkbox in the ListBox control is selected or cleared.Scroll Occurs when the scroll box on a ScrollBar control, or an object which

contains a scrollbar, is repositioned or scrolled horizontally or vertically.

Methods DescriptionAddItem To add item to the list.RemoveItem To remove an item from the list.Clear Remove all items from the list.ListCount Count the number of item in the list.List() To retrieve the item from the list according index number. ListIndex This is the index of the selected item in the list.Selected The value if True if the element is selected otherwise it’s False.SelCount Return the number of selected items in a Listbox control.

Example-01

Private Sub cmdAddItem_Click()

For a = 1 To 10

List1.AddItem "A-" & a

Next a

End Sub

Private Sub cmdRemoveSelected_Click()

List1.RemoveItem (List1.ListIndex)

End Sub

Private Sub RemoveAll_Click()

List1.Clear

End Sub

Private Sub cmdMoveAll_Click()

For X = List1.ListCount - 1 To 0 Step -1

List2.AddItem List1.List(X)

List1.RemoveItem X

Next X

End Sub

Private Sub cmdMoveSelected_Click()

For X = List1.ListCount - 1 To 0 Step -1

If List1.Selected(X) = True Then

List2.AddItem List1.List(X)

List1.RemoveItem X

End If

Next X

End Sub

Page 8: Microsoft Visual Basic 6 (Intrinsic Control)

Visual Basic Intrinsic Control

The Combo Box Control 

The function of the Combo Box is also to present a list of items where the user can click and select the items from the list. However, the user needs to click on the small arrowhead on the right of the combo box to see the items which are presented in a drop-down list.Properties DescriptionStyle Set a value that determines the type of control and the behavior of its.

The Check Box Control

The Check Box control lets the user  selects or unselects an option. When the Check Box is checked, its value is set to 1 and when it is unchecked, the value is set to 0Properties DescriptionValue Set or Return the value of control

(0=Unchecked,1=Checked,2=Grayed)

The Option Box Control

 The Option Box control also lets the user selects one of the choices. However, two or more Option Boxes must work together because as one of the Option Boxes is selected, the other Option Boxes will be unselected. In fact, only one Option Box can be selected at one time. When an option box is selected, its value is set to “True” and when it is unselected; its value is set to “False”.Properties DescriptionValue Set or Return value of an object (True,False)

The Drive List Box Control

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

The Directory List Box Control

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 File List Box Control

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 directoryMethods DescriptionDrive1.Drive Returns or sets the selected drive at run time.Dir1.Path Returns or sets the current path.File1.FileName

Returns or sets the path and filename of a selected file.

File1.Path Returns or sets the current path.File1.Pattern

Returns or sets a value indicating the filenames displayed in a FileListBox control at run time.

File1.System Return or set a value that determines whether a FileListBox control displays files with Archive, Hidden, Normal, or System attributes.

Page 9: Microsoft Visual Basic 6 (Intrinsic Control)

Visual Basic Intrinsic Control

Example-01

Private Sub Dir1_Change() File1.Pattern = "*.BMP" File1.Path = Dir1.PathEnd SubPrivate Sub Drive1_Change() Dir1.Path = Drive1.DriveEnd SubPrivate Sub File1_Click() Image1.Picture = LoadPicture(File1.Path & "\" & File1.FileName)End Sub

HScrollBar, VScrollBar, FlatScrollBar, Slider Controls

Scroll bars provide easy navigation through a long list of items or a large amount of information. They can also provide an analog representation of current position. You can use a scroll bar as an input device or indicator of speed or quantityProperties DescriptionLargeChange Set the amount of value to change when user click the scroll bar area.SmallChange Set the amount of value to change when user click the scroll bar

arrow.Max/Min Set the scroll bar maximum/min value.Value Set or Return the current value of object.

Events DescriptionScroll Occurs when the scroll box on a ScrollBar control, or an object which

contains a scrollbar, is repositioned or scrolled horizontally or vertically.

The line controls

To draw a straight line, just click on the line control and then use your mouse to draw the line on the form. After drawing the line, you can then change its color, width and style using the BorderColor, BorderWidth and BorderStyle properties.

The Shape controls

To draw a shape, just click on the shape control and draw the shape on the form. The default shape is a rectangle, with the shape property set at 0. You can change the shape to square, oval, circle and rounded rectangle by changing the shape property’s value to 1, 2, 3 4, and 5 respectively. In addition, you can change its background color using the BackColor property, its border style using the BorderStyle property, its border color using the BorderColor property as well its border width using the BorderWidth property.

Timer Control

A Timer control can execute code at regular intervals by causing a Timer event to occur.Properties DescriptionInterval Set the number of milliseconds between calls to a timer control event.

Events DescriptionTimer Occurs when a preset interval for a Timer control has elapsed. The

interval's frequency is stored in the control's Interval property, which specifies the length of time in milliseconds.

Example-01

Private Sub Timer1_Timer() Static x As Integer

Page 10: Microsoft Visual Basic 6 (Intrinsic Control)

Visual Basic Intrinsic Control If x > 5 Then x = 0 End If Shape1.Shape = x x = x + 1 Label1.Caption = TimeEnd Sub

Data Control

Provides access to data stored in databases using any one of three types of Recordset objects. The Data control enables you to move from record to record and to display and manipulate data from the records in bound controls.Properties DescriptionConnect Define the source of database.DatabaseName

Set the name and location of source database.

RecordSource Set the Table name or SQL statement.

Common properties of all bounded control as under

Properties DescriptionDatasource Set the data control which is bound to control.DataField Set a field in the current record which is binds a control.

Menu Control

A Menu control displays a custom menu for your application. A menu can include commands, submenus, and separator bars. Each menu you create can have up to four levels of submenus.