Visual Basic — FPSC Lecturer Computer Science
Preparation Notes
Complete Topic Notes | Section 8 of Subject Syllabus
Visual Basic (VB) is a Microsoft-developed, event-driven, Rapid Application Development (RAD) language that lets
developers build Windows GUI applications by dragging controls onto a form and writing code that responds to user
actions. In the FPSC Lecturer CS paper this topic is usually tested through short conceptual/definitional MCQs rather than
full code-tracing questions, so the priority is knowing exact terminology, keyword purposes, and control names cold.
1. Event-Driven Programming Concept
WHAT IT IS
Event-driven programming is a paradigm where the flow of program execution is determined by events — user actions like
clicks, key presses, mouse movement, or form loading — rather than a fixed top-to-bottom sequence of instructions. The
program sits idle, waiting in an "event loop," until an event fires and its associated event-handler code runs.
KEY POINTS
• Contrasts directly with procedural/sequential programming, where code executes strictly in the order it is written.
• Each control (button, textbox, form) can respond to multiple events; you write separate code blocks ("event
procedures") for each.
• Event procedure naming convention: ObjectName_EventName — e.g., Command1_Click(), Text1_Change(),
Form_Load().
• Common events tested in MCQs:
• Form_Load() — fires automatically when a form is first displayed/loaded into memory.
• Click — fires when the user clicks a control once.
• DblClick — fires on a double-click.
• Change — fires when the content of a control (like a TextBox) changes.
• KeyPress — fires when a key is pressed while a control has focus.
• GotFocus / LostFocus — fire when a control gains/loses input focus.
• Form_Unload() — fires when a form is closed/removed from memory.
• The "event queue" concept: Windows OS captures all user/system events and dispatches them to the appropriate
application's event handlers.
WHY VB USES THIS MODEL
GUI applications are inherently unpredictable in the order a user interacts with controls (they might click Button A before
or after typing in TextBox B), so a rigid sequential model doesn't fit — event-driven programming lets the application
respond appropriately no matter the interaction order.
COMMON EXAM TRAP
Students confuse "event-driven" with "object-oriented." VB (classic) is event-driven and only loosely object-based (it has
objects/controls with properties/methods, but classic VB6 lacks full OOP features like inheritance). [Link], by contrast,
IS fully object-oriented AND event-driven — these are two independent, overlapping properties, not synonyms. A
question asking "VB is best described as ___" almost always wants "event-driven" as the primary/defining answer for
classic VB.
2. Common Controls (TextBox, Label, ComboBox, ListBox, CheckBox, OptionButton)
WHAT CONTROLS ARE
Controls are the visual GUI building blocks placed on a VB Form from the Toolbox. Each control has Properties
(data/appearance, e.g. Name, Caption, Text), Methods (actions it can perform), and Events (things it can respond to).
CONTROL-BY-CONTROL BREAKDOWN
• Label
• Purpose: displays static, non-editable text on a form (titles, instructions, output display).
• Key property: Caption (the text shown). Users CANNOT edit a Label's text at runtime.
• Cannot receive focus / has no Click-driven data entry role — purely for display.
• TextBox
• Purpose: accepts single-line (or optionally multi-line, via MultiLine property) editable text input from the user.
• Key property: Text (holds the string the user types or the program sets).
• Common use: gathering user input, displaying editable/output values.
• Related property: PasswordChar — masks typed characters (e.g., with *) for password fields.
• ComboBox
• Purpose: a "combo" of a TextBox + a drop-down ListBox — lets the user either select from a list OR (depending on
style) type a custom entry.
• Saves screen space compared to a full ListBox since the list is hidden until the dropdown arrow is clicked.
• Style property determines behavior: Dropdown Combo (editable + list) | Simple Combo (editable + always-visible
list) | Dropdown List (list-only, not editable).
• ListBox
• Purpose: displays a scrollable list of multiple items, allowing the user to select one or more items directly (always
visible, unlike ComboBox).
• Key property: List (the array of items shown); Selected / ListIndex identifies the chosen item(s).
• MultiSelect property allows selecting more than one item at once (ComboBox does NOT support multi-select — a
common distinction MCQs test).
• CheckBox
• Purpose: lets the user select ZERO, ONE, or MULTIPLE independent options from a group — each CheckBox is
independent of the others.
• Key property: Value (0 = unchecked, 1 = checked, 2 = grayed/disabled in some versions).
• OptionButton (Radio Button)
• Purpose: lets the user select exactly ONE option from a MUTUALLY EXCLUSIVE set — selecting one
automatically deselects the others in the same group/Frame.
• Grouping matters: OptionButtons are grouped by which Frame/container they sit inside; buttons in different Frames
act as independent groups.
QUICK COMPARISON TABLE (memorize this):
CheckBox = multiple independent selections allowed.
OptionButton = only ONE selection allowed within a group (mutually exclusive).
ListBox = always visible, scrollable, supports multi-select.
ComboBox = hidden until clicked, saves space, single-select only, can allow free text entry.
OTHER CONTROLS WORTH KNOWING
• CommandButton: triggers an action/event when clicked (e.g., "Submit", "OK").
• Frame: a container used to visually and logically group other controls (especially OptionButtons).
• Timer: invisible at runtime; fires its Timer event repeatedly at a set Interval (in milliseconds) — used for clocks,
animations, polling.
• ScrollBar (Horizontal/Vertical): lets users select a value by dragging within a range (Min/Max properties).
• Image / PictureBox: display graphics; PictureBox can also act as a container and supports more events (like Click)
than Image.
COMMON EXAM TRAP
The single most-tested distinction in this subtopic is CheckBox vs OptionButton (multiple-independent vs single-
mutually-exclusive). The second most-tested is ListBox vs ComboBox (always visible + multi-select possible, vs hidden-
until-clicked + single-select). Do not mix these up under time pressure.
3. Variable Scope (Dim / Private / Public / Static)
WHAT SCOPE MEANS
Scope determines WHERE in a program a variable can be accessed/seen, and its lifetime determines HOW LONG the
variable retains its value. VB has four key declaration keywords that control this.
KEYWORD-BY-KEYWORD BREAKDOWN
• Dim
• Used inside a procedure (Sub/Function): creates a LOCAL variable, visible only within that procedure.
• Lifetime: the variable is destroyed and its value lost when the procedure ends (unless declared Static — see below).
• Used at the top of a module (outside any procedure): behaves like Private — module-level scope only.
• Most common declaration keyword; default choice for ordinary local variables.
• Private
• Used at the MODULE level (form or standard module, outside any procedure).
• Scope: accessible anywhere within that same module/form, but NOT from other modules/forms in the project.
• Used to keep a variable's use contained to just one form's or module's code.
• Public
• Used at the module level.
• Scope: accessible from ANYWHERE in the entire project — every form and every module can read/write it.
• This is the ONLY keyword that gives true global/project-wide scope.
• Should be used sparingly (global variables make debugging harder and create tight coupling between forms).
• Static
• Used INSIDE a procedure, like Dim, so scope is still LOCAL to that procedure.
• BUT: unlike a normal Dim'd local variable, a Static variable RETAINS its value between successive calls to the same
procedure instead of resetting each time.
• Classic use case: a counter that needs to remember how many times a button has been clicked, without using a
module-level variable.
SCOPE HIERARCHY SUMMARY (narrowest to widest):
Dim (inside procedure) < Static (inside procedure, but persists) < Private (module-level) < Public (project-wide)
COMMON EXAM TRAP
The #1 trap: assuming "Dim" always means global — it does NOT. Dim inside a procedure is strictly LOCAL; only
Public at module level is truly global. The #2 trap: confusing Static (local scope, but value PERSISTS across calls) with
Public (project-wide scope, always accessible) — Static variables are still invisible outside their own procedure; they
simply don't reset. A frequent FPSC-style question gives a code snippet incrementing a counter across multiple button
clicks and asks which keyword explains the counter "remembering" its value — the answer is Static, not Public.
4. Sub vs Function Procedures
WHAT PROCEDURES ARE
VB code is organized into reusable blocks called procedures. There are two types: Sub procedures and Function procedures.
Both can accept parameters (arguments), but they differ in one crucial way: return values.
SUB PROCEDURE
• Declared with: Sub ProcedureName(parameters) ... End Sub
• Performs a sequence of actions but does NOT return a value to the code that called it.
• Called simply by its name (optionally with the Call keyword): ProcedureName arg1, arg2
• Most event handlers in VB ARE Sub procedures (e.g., Command1_Click() is a Sub) — this is why button-click code
never "returns" a value directly.
FUNCTION PROCEDURE
• Declared with: Function FunctionName(parameters) As DataType ... End Function
• Performs actions AND returns a single value to the caller, using the function's own name as the "return variable"
(assign the result to the function name before it ends).
• Called as part of an expression, since it produces a usable value: result = FunctionName(arg1, arg2)
• Must specify a return data type (As Integer, As String, etc.) in modern VB, though classic VB allowed Variant by
default.
QUICK COMPARISON TABLE (memorize this):
Sub: performs action, NO return value, called as a standalone statement.
Function: performs action, MUST return exactly one value, called within an expression/assignment.
COMMON EXAM TRAP
Students sometimes think a Sub "cannot accept parameters" — it absolutely CAN; the only defining difference from a
Function is the presence/absence of a RETURN VALUE, not parameters. Also, exam questions may describe a scenario
("I need to calculate a total and use the result elsewhere in my code") and ask which procedure type is appropriate —
the answer is always Function because a VALUE is being returned and reused, not just an action performed.
5. Data Types (Variant, Boolean)
WHAT VB DATA TYPES ARE
Like any language, VB variables can be declared with specific data types to control what kind of value they hold and how
much memory they use. Two data types are particularly favorite MCQ targets: Variant and Boolean.
VARIANT
• The most flexible/generic VB data type — it can hold ANY kind of data (numeric, string, date, object, or even the
special values Empty and Null) and VB determines the actual underlying type automatically AT RUNTIME.
• It is also the DEFAULT type: if you declare a variable with Dim x (no "As Type" clause), VB automatically makes it
a Variant.
• Trade-off: maximum flexibility, but uses more memory and is slower to process than a strictly-typed variable, since
VB must track and check the type dynamically.
BOOLEAN
• Holds only one of two logical values: True or False.
• Internally, VB stores True as -1 and False as 0 (a commonly tested numeric-internals fact).
• Used for flags, condition results, and toggle states (e.g., IsValid, IsChecked).
OTHER VB DATA TYPES WORTH KNOWING (quick reference):
Integer — whole numbers, smaller range/memory footprint.
Long — whole numbers, larger range than Integer.
Single / Double — floating-point (decimal) numbers; Double has greater precision.
String — text data.
Date — stores date and/or time values.
Currency — fixed-point number optimized for monetary calculations, avoiding floating-point rounding errors.
Object — a reference to any object (form, control, or external object).
COMMON EXAM TRAP
The most common trap is not knowing that Variant is BOTH "can hold any data type" AND "the default type when no
data type is explicitly declared" — FPSC-style questions often test the second fact alone ("If no data type is specified
when declaring a variable in VB, it becomes a ___ by default"). The second trap is the True/False internal numeric
representation of Boolean (True = -1, not +1) — a frequently tested specific numeric fact.
Visual Basic Quick-Revision Checklist
✓ VB = event-driven, RAD language (classic VB6 is NOT fully object-oriented; [Link] is).
✓ Event naming pattern: ObjectName_EventName (e.g., Command1_Click).
✓ CheckBox = multiple/independent; OptionButton = single/mutually exclusive (grouped by Frame).
✓ ListBox = always visible + multi-select possible; ComboBox = hidden dropdown + single-select, saves space.
✓ Dim = local; Static = local but retains value between calls; Private = module-level; Public = project-wide/global —
ONLY Public is truly global.
✓ Sub = action only, no return value; Function = action + MUST return one value, used in an expression.
✓ Variant = holds any data type, is the DEFAULT type if none specified; Boolean = True(-1)/False(0) only.
✓ Key files: .frm (form), .bas (module), .vbp (project).
✓ Key operators: \\ (integer division), Mod (remainder), ^ (exponentiation).