Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Chapter 2 Introduction to Visual Basic Programming Visual Basic.NET
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi 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
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi 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
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi The IDE Profile
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Starting the IDE The Visual Studio Start Page
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Starting a New Project Default: Windows Application
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi The Integrated Development Environment (IDE) Menu bar provides many menu items – Examples include File and Edit menus Toolbar lies below menu bar – Provides shortcuts to the menu bar Toolbox – Contains various icons representing VB controls Solution Explorer – Shows all forms, references, classes, and modules the current project contains
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi The IDE Menu bar Toolbar Blank form in IDE Toolbox
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi 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
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Your First Visual Basic Program Form contains a label and a button Double-click the button on the form to open the code window
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi The Code Window Tabs at top let you toggle between the form and the code window Comments appear green in the IDE dot (period) Object-oriented syntax: object and property separated by dot (period) Procedure box lists events the object in the object box recognizes Object box lists all objects in the form
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi When You Run the Program Label changes when you click the Say Welcome button
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Understanding the IDE Design time – When you are placing controls on the form code window – 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
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Coding Mechanics Comments – Used to provide clues to the purpose of the code – Begin with tick mark (apostrophe) or Rem – Appear green in the IDE Showing Blocks of Code (Lists) – 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
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Interfaces of VB Objects 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
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Properties Special types of data associated with object Most relate to appearance of objects – i.e. Label1.BackColor = Color.Red (Try) Some relate to behavior of objects – i.e. Try the Properties Tab Object and property separated by dot (period) Property must be given a value
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Events User or system actions the object recognizes – i.e. a button has a Click event Event procedure – 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
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi 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
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi 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
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi 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 before Name objects before placing code in the code window
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Assignment Statements Generate some sort of result – i.e. moving data from one location in memory to another Syntax: lblWelcome.Text = “Welcome” – Expression on right hand side of equation moved to memory location on left hand side Most common statements in VB programs
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Statements That Direct Execution Flow Conditional Execution – One block of code executed if a statement is true; a different block executed if false – If block is most common example Repetition – When you need to execute a block of code repetitively – For…Next loop is most common Code that “Jumps” – Exit Sub exits a procedure
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi 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
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi 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
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Summary Start the IDE from the Start Menu – Choose Visual Studio.NET from Programs 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 – Code window behind form is where you place code Solution Explorer shows all files in your solution
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi 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
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi 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
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi 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