Chapter 2 Introduction to Visual Basic Programming
Visual Basic.NET
Objectives
Navigate the IDE Appreciate the use of a form Create controls on a form and adjust their sizes Understand the events, properties, and methods of controls Understand how the code and events work in a VB program
Objectives
Open and save a VB project Understand the coding mechanics and the naming convention Get help from the MSDN help system Enumerate(Number) the types of statements in a program Run an executable without the IDE
The IDE Profile
Starting the IDE
The Visual Studio Start Page
Starting a New Project
Default: Windows Application
The Integrated Development Environment (IDE)
Menu bar provides many menu items –
Toolbar lies below menu bar –
Provides shortcuts to the menu bar
Toolbox –
Examples include File and Edit menus
Contains various icons representing VB controls
Solution Explorer –
Shows all forms, references, classes, and modules the current project contains
The IDE
Menu bar
Toolbar
Toolbox
Blank form in IDE
Solution Explorer and Properties Window Solutions Explorer shows all the forms, references, classes, and modules in the project Properties window shows all the properties of the selected object
Your First Visual Basic Program
Form contains a label and a button
Double-click the button on the form to open the code window
The Code Window Tabs at top let you toggle between the form and the code window Procedure box lists events the object in the object box recognizes
Object box lists all objects in the form
Object-oriented syntax: object and property separated by dot (period)
Comments appear green in the IDE
When You Run the Program
Label changes when you click the Say Welcome button
Understanding the IDE
Design time – –
When you are placing controls on the form When you are writing code in the code window
Runtime –
When the code in your project comes to life, responding to events
Press Start on the Debug menu in the IDE Press the F5 key in the IDE Press the start button in the IDE
Coding Mechanics
Comments – – –
Showing Blocks of Code (Lists) –
Used to provide clues to the purpose of the code Begin with tick mark (apostrophe) or Rem Appear green in the IDE
The IDE will help indent the code lines
Line Continuation –
If you have to write a long statement, break into lines by using the underscore ( _ ) character
Interfaces of VB Objects
Forms and controls are objects Objects have interfaces: –
Properties: typically relate to appearance of
–
objects Events: user or system actions recognized by the object
–
Procedures written to handle events
Methods: actions that objects are capable of performing
Properties
Special types of data associated with object Most relate to appearance of objects –
Some relate to behavior of objects –
i.e. Label1.BackColor = Color.Red (Try)
i.e. Try the Properties Tab
Object and property separated by dot (period) Property must be given a value
Events
User or system actions the object recognizes –
Event procedure – –
i.e. a button has a Click event
Procedure written to handle a specific event Also called event handler
Syntax: Private Sub ObjectName_Event – – –
Code surrounded by Sub … End Sub Private refers to the scope of the procedure Object and event separated by underscore
Methods
Actions objects are capable of performing –
i.e. Me.Close()
Syntax: Object.Method(Parameter List) – –
Parameter list: arguments passed in to the method Parameter list must be enclosed in parentheses, even if no parameters are required
Getting Help from the Help Menu Enter search term in the Look for box
Double-click an item in the list box
Double-click an item in the lower center pane; upper center pane shows results
Naming objects
When naming objects, be descriptive – –
Use standard prefixes, i.e. lbl for label Give the object a meaningful name
i.e. a label with text “Name” could be lblName
Change the object name in the Properties
window
Name objects before placing code in the code window
Assignment Statements
Generate some sort of result –
Syntax: lblWelcome.Text = “Welcome” –
i.e. moving data from one location in memory to another
Expression on right hand side of equation moved to memory location on left hand side
Most common statements in VB programs
Statements That Direct Execution Flow
Conditional Execution – –
Repetition – –
One block of code executed if a statement is true; a different block executed if false If block is most common example
When you need to execute a block of code repetitively For…Next loop is most common
Code that “Jumps” –
Exit Sub exits a procedure
Completing the Development Cycle
Program must be compiled into executable –
When you test run your program, the IDE compiles and saves the executable
To run the program: – – –
Double-click the executable Use the Run option off the Start menu Add the executable to your Start menu
Finding the Executable
Executable is found in the /bin folder underneath the folder containing your project. In this case: C:\My Documents\ Visual Studio Projects\Welcome
Summary
Start the IDE from the Start Menu –
Edit your profile to customize environment Menu bar gives you all menu items. Toolbar offers shortcuts to menu items Toolbox contains controls and components The form allows you to design visual elements –
Choose Visual Studio.NET from Programs menu
Code window behind form is where you place code
Solution Explorer shows all files in your solution
Summary
Properties window shows properties of selected object To develop a program – – –
Draw and adjust the controls on the form Set properties of the objects Place code in events, then test and revise
The IDE allows you to write and compile the program Use comments in your code to show the purpose of the code Break long code lines with an underscore character
Summary
Objects have three types of interfaces: properties, methods, and events VS.Net help file provides a wealth of information Follow object naming conventions – –
Use predefined prefixes for objects Give objects meaningful names
Two types of programming statements: –
–
Those that produce results Those that control program flow
Summary
Four types of objects introduced: – – – –
Form used for visual interface Label used to display text to give clues Button used to trigger actions Timer used to keep track of time
IDE automatically produces the executable when you develop the program Run the executable like other Windows programs