Intro to VB: Introduction • Visual Basic is derived from the BASIC language (Beginner’s All-purpose Symbolic Instruction Code – Created in 1964 by John G. Kemeny and Thomas E. Kurtz at Dartmouth – Imperative – Interpreted • Visual Basic evolved from QuickBasic in 1991 – Object-oriented – Can be interpreted or compiled – When interpreted, source code converted (compiled) into an intermediate code ∗ The intermediate code is platform independent ∗ Intermediate code interpreted on a given platform – When compiled, result is object code for a specific platform • VB is intended for GUI (Graphical User Interface) programs – GUI program is window-based – User is presented with one or more controls with which to interact with the program; e.g., ∗ ∗ ∗ ∗

Drop-down menus Text boxes Radio buttons etc.

– Order in which program executes depends - in part - on interactions of user ∗ This style of program is referred to as event-driven ∗ Steps in program execution: 1. System (VB) monitors controls to see if an event has occurred 2. If an event has occurred, check to see if an action has been specified 3. If an action has been specified (a) Execute the action (b) Go to step 1 4. Go to step 1

1

Intro to VB: VB Programs • VB program consists of a hierarchy of objects: 1. Project – Collection of files that together make up a VB program 2. Form – Basic window used to design a VB interface – Represents screen layout on which controls are placed 3. Controls – Basic objects used in VB – Provide the means by which a user interacts with a program – E.g., menus, dialog boxes, text boxes, radio buttons, ... 4. Properties – Attributes that determine characteristics of objects – E.g., size, font, color, ... 5. Events – Actions that allow user interaction with programs – E.g., mouse click, key stroke, dragging, ... 6. Event procedures/event handlers – Methods defined by programmer for responding to user actions – These are where most code writing occurs • The general steps involved in writing a GUI-based VB program: 1. Design the interface – Determine what controls are needed – Position controls on form – Set attribute (property) values for controls 2. Determine what events will trigger each control 3. Write event handlers for each event

2

Intro to VB: Creating a Project • Visual Studio (VS) is programming environment • Is an Integrated Development Environment – – – –

IDE provides editor for writing program Compiler Utilities to help program development Windows-based

• VS is IDE for several MS languages: VB, Visual C++, C#, .. • On starting VS, presented with the start page

3

Intro to VB: Creating a Project (2) • To create a new project, use New Project, (or File/New/Project when not working with the Start Page) 1. Select Other Languages 2. Select Visual Basic 3. Select Windows 4. Select Windows Forms Application

5. Provide a name for the project – Usually, create a folder for the project beforehand

4

Intro to VB: Creating a Project (3) • Project file has extension .sln – Contains info for combining and coordinating all the files that make up your project

5

Intro to VB: IDE Form Design Mode • Once project created, the IDE displays in form design mode

6

Intro to VB: IDE Form Design Mode (2) • Menu Bar and Tool Bar include standard Windows buttons along with those for programming • Solution Explorer shows files and components of program • Document Window holds other windows • Form Design Window is used to design the layout of a GUI program • Tool Box provides access to VB controls • Properties Window shows attributes and their values for selected program object • Output Window displays output when program executes • Code Definition Window used for editing code

7

Intro to VB: Forms • Form represents a windowed interface for program • Initially is blank • GUI program created by adding controls • By default, form named Formn.vb • Program may use several forms • Each form saved in its own file E.g., Form1.vb • 2 windows are associated with a form: – Form Design Window for adding controls and arranging their visual appearance – Code Design Window for adding program code

8

Intro to VB: Controls • Controls are objects that allow user to interact with program – For example (from this chapter) 1. Text box: For entering and editing text 2. Label: For labeling other GUI objects ∗ User cannot change text 3. Button: Allows user to initiate an action 4. List Box: Allows user to select one item from a list (e.g., a menu) ∗ Used to display output in this chapter • Controls are added to a form from the Tool Box

– Add by 2X clicking or dragging • Selected control is one that can be manipulated – Only 1 control is selected at any time – Know it’s selected because it has resizing handles displayed

9

Intro to VB: Controls (2) • Control properties – Determine characteristics of control – Properties of selected control may be changed by entering values in the Property Window

– Properties like size and location can be changed by dragging – Properties may also be changed by a program as it executes (details later) – Description Pane appears at bottom of Properties Window ∗ Displays brief description of property currently highlighted • Arranging controls in form – Can be placed by changing values in property window or dragging – When dragging location, alignment bars appear to facilitate alignment

10

Intro to VB: Controls (3) • Multiple controls – Can select several controls at once (CRTL+Click, drag an area, Edit/Select All) – One with white handles is master control ∗ What is done to it will affect all selected – Can align multiple controls relative to each other from the format menu/toolbar

– By repeatedly hitting the TAB key, you can cycle thru the program elements ∗ This order can be changed using View/Tab Order • Object names in VB – By default, VB names program items with the name of the object followed by a number; e.g., Form1, TextBox3, Button2

11

Intro to VB: Controls (4) – The following convention is recommended for VB names: ∗ Start with a 3-letter prefix, all lower case ∗ VB uses standard set of prefixes, each associated with a particular object type: Control prefix Form frm Button btn Combo box cbo Check box chk Command button cmd Common dialog dlg Data dat Data-bound combo box dbc Directory list box dir Drive list box drv File list box fil Frame fra Form frm Horizontal scroll bar hsb Image img Label lbl Line lin List box lst Menu mnu OLE ole Option button opt Picture box pic Shape shp Text box txt Timer tmr Vertical scroll bar vsb ∗ This is followed by a meaningful name; e.g., btnStartButton, lblStartButtonLabel

12

Intro to VB: Controls (5) • Button access key – An access key allows a user to press ALT+key to activate a button – To create an access key: 1. Access the Text property of the button 2. Place a & in front of the letter you want to use

13

Intro to VB: Opening, Running, and Saving VB Programs • To open an existing VB program – File/Open Project/Solution • To close a VB program – File/Close Solution • To save a VB program – File/Save All • To execute a program – Start button (triangle) – Debug/Start Debugging • To halt execution of a program – Stop button (square) – Debug/Stop Debugging • To email a program – Save the project – Navigate to the folder where the project is stored – zip the entire project folder (try right-clicking on the folder) – Send the zipped file

14

Intro to VB: Events and Event Handling • Event is an action initiated by the user – E.g., button click, entering text, ... • In a GUI program, controls have events associated with them – Programmer determines what events program recognizes, and what actions to take when they occur – Programmer writes code to carry out the actions – This code called an event handler • In VB, event handlers are implemented using a subroutine – Subroutines are also called procedures in many languages, and methods in object-oriented languages • Event handlers are methods associated with control objects • When writing subprograms, VB will automatically supply some of the code • Event handler syntax: scope Sub subroutine name (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles event, event, ... statements End Sub – The header is the first line of code (scope Sub subroutine name (...) Handles event, event, ...) ∗ It contains info needed by program to access the handler · The name of the subroutine (subroutine name) · The event(s) this subroutine handles (event, event, ...) · The object that triggered the event (sender As System.Object) · e Additional info about the actual event sent by the sender (e As System.EventArgs) · Where/what can access this subroutine (scope) – The body contains the code that is executed by the handler (statements) ∗ This is the code that the programmer supplies

15

Intro to VB: Events and Event Handling (2) • Naming conventions – Names for event handlers use the following convention: objectName eventName, where ∗ objectName is the name of the control ∗ eventName is the type of event E.g., btnStart Click – Names for events use the following convention: objectName.eventName E.g., btnStart.Click • Example: Private Sub btnTurnBlue_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTurnBlue.Click txtBox1.ForeColor = Color.Blue txtBox2.ForeColor = Color.Blue Me.ForeColor = Color.Blue End Sub

16

Intro to VB: Writing Event Handlers • Code is written in the Code Editor – Accessable by a right click/View Code in Design Window, or View/Code from Menu Bar

– Note that VB provides skeleton code for you – What is initially displayed is the skeleton for the class definition for the form you clicked in: Public Class form name End Class

17

Intro to VB: Writing Event Handlers (2) • To create an event handler for a control: 1. Type it from scratch directly in the Code Editor 2. Double click the control in the design window – This puts a skeleton code for the default event associated with the control 3. Access the Property Window for the selected control – Choose the event button (lightning bolt icon) – Select the event you want

• One of most basic statements is an assignment statement – Syntax: variable name = value – Semantics: Value of right side is copied into memory location associated with variable – For now, the variable will be an object’s property ∗ Denoted as objectName.propertyName E.g., txtBox1.ForeColor = Color.Blue – Note that if a value is text, it must be delimited by double quotes

18

Intro to VB: Writing Event Handlers (3) • Self-reference – Objects cannot make references to themselves in VB using normal syntax E.g., form1.att = value is not a valid assignment from within form1 – For self reference, an object uses Me for the object name E.g., Me.att = value • Note that if change the name of an object in the Design Window, its name will be automatically changed in the Code Window

19