- Object Oriented Programming concepts
-
- Encapsulation: The object appears to be wrapped up. Only variables and routines
(Subs/Functions) that were declared Public would be available to outsider.
-
- Inheritance: A class that inherits from another class would have the same
characteristics as those of the parent class. In VB6, an ActiveX derived from another
ActiveX does not naturally inherit everything from the parent ActiveX. However, you can
use the VB ActiveX Control Wizard to effectively achieve that aim. That is, it’s
necessary to explicitly declare all the required properties and methods as being the same
as those of the parent ActiveX.
-
- Polymorphism: Method of the same name means different thing in different
situation.
-
- Instantiation: The process of creating an object of a particular class.
-
- Menu
- Use the right Arrow to move a menu Item to the next level
- HelpContextID is used for context sensitive help
- Visible: Hide the Menu item by making its Visible=False
- Enabled : The menu item is grayed if Enabled=False
- Shortcut: Equivalent Key stroke of the menu command
- Checked is used to indicate the status of a flag.
- PopUpMenu
- This example displays a pop-up menu at the cursor location when the user clicks the
right mouse button over a form. To try this example, create a form that includes a Menu
control named mnuFile (mnuFile must have at least one submenu). Copy the code into the
Declarations section of the form, and press F5.
- Private Sub Form_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As
Single)
- If Button = 2 Then
- PopupMenu mnuFile
- End If
- End Sub
-
-

-
- Form
-
- Load: Use the Form_Load event for initialisation. Initialisation means things you
want to have ready before using the Form, such as fill up a combo box with data, assign
initial values to certain variables.
- When the program starts up, it runs the Sub Main from the Basic Module or load a Form of
your choice. Use the Menu Command Project | Properties (Click Tab GENERAL) to select the
start up Form or Sub Main:
- While running the application, you can get the application out of sight by using the Hide
method, and bring it back with the Show method. Hide/Show does not affect the
status of the form at all because the form is already there.
- You can remove the form completely from the memory by using the Unload method,
then bring it back again with the Show method. This time the Show method implies a Load
action plus the normal Show action (similar to the case Hide/Show), i.e. the Form is
initialised by the Load action.
- Sometime you want to do some initialisation on the controls of the form before
presenting it to the world. In that case do a Load, do all the necessary initialisation
then Show the form.
- Note that while you are inside Sub Form_Load the Form is not yet existent. Therefore you
cannot execute a Setfocus of a control of the form while that form is being loaded. One
way to get around it is to Enable a Timer to schedule that Timer to run in 300ms (i.e.
delaying the SetFocus action until the Form becomes existent).
-
- Sub Form_Load
- ‘ Put code to initialise the Form here
- Timer5.Enabled = True
- End Sub
-
-

-
- In the Timer routine ask Text1 to seize the cursor focus. Remember to disable Timer5 so
that it won’t run again.
Private Sub Timer5_Timer()
Text1.Setfocus ‘ Give Text1 the cursor
Timer5.Enabled=False ‘ Stop Timer5
End Sub
-
-

-
- If you want to define the start up position of a Form (i.e. where the Form appears on
the screen when it first shows up at run-time) use the Form Layout Window to do that.
- Activate: use Form_Activate to update certain things each time the form is
revisited. It’s similar to initialisation in Form_Load, except that Form_Load is
called only once at loading whereas Form_Activate is called every time the Form becomes
active (i.e. regaining the focus).
- Unload ' use to tidy up loose ends before leaving. Sometimes, it’s better to
use Form_QueryUnload as it returns UnloadMode which tells you how the unload was
initiated (i.e. by user, by other program etc..). Then you can decide to stop the Form
unload process by setting Cancel to True.
-
- Left, Top: X,Y coordinates of Form relative to the Top Left corner of the screen.
You can set these values in Form_Load.
- Width, Height : Size of Form including Border. Use ScaleWidth and ScaleHeight
to refer to the form size without border.
- WindowState : You use it to indicate the start up state of the Form.
It’s either Normal (as you had it at design time), Minimised (Iconised) or Maximised
(take the whole screen).
- A form can be run in either Modeless or Modal mode.
- Example:
‘Form3 will take over Mouse/Keyboard input and nothing else in this application
runs until Form3 unloads.
Form3.Show vbModal
Form2.Show vbModeless ‘ Form2 has the focus but
it’s just another form.
If you want Form2 to always stay on top of this Form (the form that runs the code) use:
Form2.Show vbModeless, Me ‘ Me is this Form,
it’s declared as Parent or Owner of Form2.
- MDI Form: like MSWord, always stays behind its MDIChild forms even when you click
its menu
- MDIChild ' Any form can be an MDIChild of an MDIForm
- Hide ' You can't hide an MDIChild form but you can position it out of sight
- Show implies Load and Show
- Beware:
- When a Form is referred to by any code its Form_Activate is executed.
- ShowInTaskBar
-
- Frame Control
- OptionButton Control
- Check Control
-
- Label Control
- Autosize ensures the whole content is displayed.
- Alignment: Left, Right or Center. Align a group of Labels by selecting them all
the use the Menu command Format. Select a group of controls by dragging the mouse to draw
a rectangle around the wanted controls. If the group is inside a container such as a Frame
or a PictureBox, select the Form first, then hold the Ctrl key down while dragging
the mouse to draw a rectangle around the wanted controls.
- Apprearance: Flat or 3Dimensional
- BackStyle: Opaque or Transparent
- BackColor
- ForeColor
- Hint:
- You can blink a Label by using Timer Control
- Eg: Assumming you have a Timer1 control Enabled with Interval=500 msecs.
-
- Private Sub Timer1_Timer()
- Static BlinkOn ‘ This value stays around after each
call of Timer1_Timer
- If BlinkOn then
- LblLabel.Caption= ""
- Else
- LblLabel.Caption= "Catch me if you can!"
- End if
- BlinkOn = NOT BlinkOn
- End Sub
-
-

-
- If you want to move the cursor to a text box by using Alt-Key (eg: Alt-N) place a label
in front of the text box in TabOrder with letter N underlined by &.
-
- TextBox Control
-
- Font let you modify Font name and Style.
- When you place a new Text box onto a form, its font characteristic is the same as that
of the form. Eg: If the Form’s font is Courier 10 Bold, the text box’s font
would also be Courier 10 Bold.
- Use Sendkeys function to emulate the entry of keystroke.
- Example:
Text1.Setfocus
Sendkeys "Hello there!" ‘ Type the
string "Hello there!" in Text1.
Text4.Setfocus
Sendkeys "{END}" ‘ Move the cursor to
the end of the text string in Textbox Text4
- Alignment: Left, Right or Center. Only work when Multiline is True
- PasswordChar: Use PasswordChar = "*" to hide password when it is
entered.
- Multiline enables display of many lines of text. Use CTRL+Enter to add a new line
when entering text at design time. The PasswordChar does not work if Multiline is true.
- Enabled: If Enabled is False the display is grayed and you can't enter data
there. Try Lock property if you want it to display clearer.
- TabIndex controls order of Tab Stop. Arrange the order of Tab Stops among the
text boxes with TabIndex. If you don’t want cursor to stop at a textbox set its TabStop
to False.
- SetFocus method can be used to bring the cursor to this textbox.
-
- CommandButton Control
-
- Enabled: When Enabled = False the button is grayed, and user can't Click it
- Visible: Makes the Command button visible or not
- ToolTipText: Use this to give brief description of what Clicking the button would
do
- Picture: You can load a picture onto the button to make it look more interesting.
The Picture’s info is stored in the graphic file of the form (extension *.FRX ).
-
-

-
- Hint:
- When you print a Form itself, you can make undesirable Commandbuttons invisible before
printing, then restore their visibility afterward.
-
- Timer Control
- Use Timer to do something periodically like updating a clock, checking something to see
if it's necessary to do something in real-timeẨ
- Or to schedule to do something once only at a later time (see Hint).
-
- Enabled is TRUE makes the Timer active
- Interval When Timer.Interval (in milliseconds) has elapsed the Timer Sub is
executed.
- Hint:
- If you want the Timer1 execute once only then insert the statement
- Timer1.Enabled = False
- at the beginning of the the Sub Timer1_Timer
- If you can't do something in Form_Load because the Form is not yet available, use a
Timer to schedule that action about 300 msecs later when the Form becomes ready.
- You can trigger an action in another Form by Enabling a Timer in that form. This is
effectively Real-time response from the other Form except that no parameters are passed.
You can get around this by storing data in a Basic module that can be accessed by all
forms.
- Note that Timer function is different from Timer control. The Timer function returns the
number of seconds since midnight.
- If you use Timer function to wait for a time period, beware of the wrap around at
mighnight.
- Eg:
- Dim Start!, EndTime!
- Start! = Timer
- EndTime! = Start! + WaitPeriodInSecs!
- Do
- DoEvents
- If Start! > Timer Then
- '-- Adjust for Midnight
- EndTime! = EndTime! - 24 * 60 * 60
- End If
- Loop Until Timer >= EndTime!
- ‘ Wait period has elapsed. Now do something
-
- Image Control
-
- Stretch is TRUE lets you increase or decrease the size of the image horizontally and
vertically.
-
- ListBox and ComboBox Controls
-
- ListIndex referred to the line you just clicked.
- The (i+1)th line of the listbox is given by List1.List(i), ie. The
lines are counted from 0 to Listcount-1
- Listcount
- Sorted ensure all items are automatically sorted into alphabetical order. This
property can only be set at design time.
- Hint:
- You can use an invisible (ie. Property Visible = False) Listbox to help sorting another
Listbox.
- This is done as follows:
- - Clear Invisible ListBox and Set its Sorted property to TRUE
- Copy everything from Original Listbox to the invisible ListBox where items will be
sorted automatically.
- Copy everything from invisible Listbox back to the Original ListBox
- Multiselect lets you select several lines in the list box.
- Use SHIFT to select a range, use CTRL to select individual lines.
- List1.ItemData(i) can only store a number (not string)
- When a line is highlighed its List1.Selected(i) is TRUE
- Clear
- AddItem
- RemoveItem
- Beware:
- When ListIndex of List1 is assigned a value like in
- List1.ListIndex = 0 the List1_Click Sub is also executed.
- Listcount is changed after each AddItem or RemoveItem
- Examples of application of Listbox:
- Displays content of a text file
- Displays selected items from other lists
- Sort items into alphabetical order
|