- VB Controls
-
- Each control has Properties, Methods and Events.
-
- 1. Properties
- Properties are what belong to the control, its data, its
characteristics. When you assign a property value to a control it is actually a call of a
Sub, not just a simple passive assignment. Eg: Changing Font or ForeColor, you see the
result immediately.
- Some properties can be assigned at Design time. Others can only be
assigned at run-time, i.e. by coding.
- You change Properties of a control at design time by using the
Properties Window or the Properties Pages.
- Some of the popular properties are:
-
- Visible: If Visible = FALSE the control is invisible. So you
can blink an object by alternating its visible property between True and False every 500
ms.
-
- Enabled: If Enabled=False the object is grayed and does not
respond to any mouseclick or keystroke. So to make a Commandbutton unavailable you set its
Enabled property to False.
- Example:
CmdSave.Enabled = False ‘ this greys the Save button
-
-
- Properties Pages of a ToolBar
-
- Left, Top, Width, Height: Left and Top are the X,Y coordinates
of the control relative to its container (not necessarily its Form). Width and Height give
the size of the control. An efficient way to change these values is by using the Move
method .
- Eg:
newX=100 : newY=50 : newWidth=3000 : newHeight=2000
MyControl.Move newX, newY, newWidth, newHeight
If you just want to move the form without changing its size then
just leave the size data out:
MyControl.Move newX, newY
-
-
- Sometimes you cannot find where a control is on its Form, locate it
using the Properties Window (click the down arrow on the right of the combobox at the top
of the Properties Window to see the list of all controls on the form.) then look at its
Left and Top values. One of them may be too big or negative which places the control
outside the form (i.e. out of view). Change that crazy value to bring the control back to
earth.
-
- TabIndex: As you press the Tab key, the cursor moves the focus
of the Mouse/Keyboard Input from one control to the next. You define the order by which
the cursor moves among the control by assigning a TabIndex value to each control. So if
presently the cursor is on the control with TabIndex equal 3, when you next press the Tab
key the cursor will move to the control whose TabIndex is 4. The cursor does not stop at a
label, a control that is locked or a control whose TabStop value is False.
-
- 2. Methods
- Methods are what the control can do. You ask the control to do
something by itself when you call the control's method with some optional parameters.
- Eg: The following code adds a line of text to Listbox List1
List1.Additem "This is the test line of text"
- For example some of the methods of a Listbox are Clear, AddItem,
RemoveItem
-
- 3. Events
- Events are provided as available hooks for you to use. For example,
you want to check the data entered
- at a text box as soon as user finishes with it. In that case you can
use the LostFocus event.
- Example:
-
- Sub TxtAge_Lostfocus()
- If Val(txtAge.text) > 100 then
- MsgBox "Warning: This person appears too old."
- End if
- End Sub
-
-
- Click To write code for the Click event of a Commandbutton,
doubleClick that button, VB will bring you to the Code window and position the editing
cursor at the right place after generating the declaration of the Event Click Sub.
- You can call the Sub ControlName_Click like a normal Sub because it
does not require any parameters. This will emulate someone clicking the Control.
- Eg:
Call CmdShowTime_Click
- You can find out what events are available for a particular control
by clicking the Combobox on the right in Code Window after selecting the control. Those
events that have code are in bold, eg: Click in this case.
-
-
- Above is the list of Events available for the
Commandbutton CmdConvert.
-
- Each Form has two kinds of routines (i.e. Sub/Function): the Event
and the General ones. If you removed a control that has an Event routine that Event
routine still exists as a general routine.
-
-
- Some controls like Frame, PictureBox are container controls.
Form itself is a kind of container control. A container control can contain other
controls. When you move a container all the controls contained in it are also moved. Note
that it’s possible to have a control sitting on top of a container control but does
not belong to (contained by) the container control.
-
- Mousedown (used for Drag) ' Use MouseDown/MouseUp when Right
Click or combine Click with SHIFT or CTRL. To find out what key was hold down, check the
Button and Shift values.
- Each Click event also has a MouseDown and a MouseUp event. MouseDown
and MouseUp events also return the coordinates (position) of the mouse relative to the top
left corner of the control’s container.
-
- KeyPress
- Example:
Private Sub Text1_KeyPress(KeyAscii As Integer)
' Treat Enter key like TAB key
If KeyAscii = 13 then
KeyAscii = 0 ‘ Eliminate the Enter keyascii to avoid side
effect.
Sendkeys "{TAB}" ‘ Emulate pressing the Tab key
End If
End Sub
- Certain keystrokes are not captured by KeyPress. In that case use KeyDown
event which returns both the Keycode and Shift.
- Change
- Example:
Private Sub Text1_Change()
CmdConvert_Click ' Emulate a click of CommandButton CmdConvert
End Sub
- Use the Change event of a Textbox carefully to avoid the Change Event
Sub calling itself.
- GotFocus ' gets the cursor. Opportunity to tell user that the
program is ready to receive next input keystroke. For example, change the backcolor of the
textbox.
- LostFocus ' The cursor is moving to the next control in
TabIndex order. You can check validity of entered data on LostFocus event
- DragDrop ' Implement action for the Control on which another
Control has just been dropped. Use DragOver to give visual feedback to user that a Drop
can be carried out on the target Control. Don’t forget to assign a DragIcon to the
Control that is being dragged, otherwise the outline of the whole control will drag.
- VB Project
- The VB project file (.vbp) is a text file. It contains:
- the list of ActiveXs and References that are required by the
application
- the list of Forms, Modules and Classes etc
- all other information about the project. Any of these can be changed
by using the Project | Properties menu command.
-
Form ASCII file is stored as a text file, extension *.FRM . It has
two parts:
- the description of the Visual Components. Basing on this information
VB can reconstruct the picture of the form with all its controls on the screen.
- the MSBasic Codes.
-
- The Visual Control description part may include Menu details.
- Form Graphic: File extension *.FRX. This file type contains the
graphic part such as Picture, Icons of the form.
- Modules has no Visual Control parts. It is visible by all Forms, ie.
Any form can refer to the module's variables and Subs/Functions provided these items are
declared Public.
-
|