Visual Basic 2008 The programming part Code Computer applications are built by giving instructions to the computer. In programming, the instructions are called statements, and all of the statements that make a program function are called the program code. Visual Basic is an Object Oriented Programming (OOP) language. OOP code is organized into classes, each class defining a set of data and actions. Classes are used to generate objects. You used Control classes to create interfaces already. Event Procedures Events are the actions that occur when the application runs. An event can be a mouse click, a keystroke, or a control response. Once you design GUI, you must write code that responds to events. In a way, each event procedure is like a miniature program that you write for each control whose event should cause on action to take place. Procedures perform specific tasks in response to a user interaction with an object and are written within a class. Code Statements Code statements are written in the Code window, using Visual Basic's own programming language syntax (which is similar to Liberty BASIC). When you open the Code window you will find code already in place such as: Syntax: Form Public Class Form1 End Class To open the Code window, click on View Code or select the View Code button on the Solution Explorer window.
Class statement: used to define a class Begins with Public Class <class name> Ends with End Class Class Name list box: lists the names of objects (controls) included in the user interface Method Name list box: lists the events to which the selected object is capable of responding When you select a control from the Class Name list box and a method name, a code template for the event appears in the Code Editor window. The class will need statements that will tell the computer what it is to do before your program will do anything. (A class by definition is a data type that can store data and includes a set of statements that perform a well defined task). Procedures A procedure is a block of code written to perform the specific tasks of a program solution. In a way, each event procedure is like a miniature program that you write for each control that needs to respond to the end user of the program. Event procedures are added to a class and contain no statements to begin with, just a blank area where the programmer will code statements. In the Code window, event procedures can be added by selecting the object name in the Class Name list and the event from the Method Name list. In Designer window, double clicking on the object will open the Code window with an event procedure that selects the most common method for that object. For instance, the mouse common method for a Button is the click event, which executes in response to a mouse click. For example: Syntax: Procedures Public Class frmmainform Private Sub ObjectName_Event(ByVal sender As Object, ByVal e As System.EventArgs) Handles Object Name.Event End Sub End Class Statements
Private indicates that the procedure cannot be seen outside of the frmmainform class. ObjectName is the selected object, it could be a control or a form. Text boxes are one class of objects. Event is the event name, it could be a click (mouse click). Sub declares the procedure. End Sub is required to end the Sub statement. The procedure heading that is generated by VB contains a set of parentheses with arguments that are used by the procedure. It is extremely important to not change the arguments at this time. Every program must contain commands for input and output in order to be interactive. Well written code is clear and easy to understand. This makes debugging and modification easier. The IDE automatically indents code for readability, but you may use the Tab key as we.. Properly indented code is good programming style. Methods A method is a procedure in a class. Visual Basic includes several built in methods for use within your application. These predefined instructions can be used to manipulate data or terminate a program. Commenting code In order to make maintenance of your code more manageable, include comments. Commenting vague or misleading code can be accomplished using single quotation marks ( ) to begin the comment.
Lab #2 Adding Code Open the Wizard Application solution that you created in Lab #1 Open the Code window From the drop-down menus, select the cmdexit Button and the Click Event. To close a form in a running program, the syntax Me.Close ( ) will accomplish this. Me is a keyword that refers to the current form. The Close Method closes the form. This will terminate a program if it has just one form. To end the running of a program, the syntax Application.Exit ( ) will accomplish this. Application refers to the application that you have created and the Exit Method closes out the program. In between the Private Sub and the End Sub Statements, type: Me.Close ( ) Run the program and click on the Exit button to ensure that this code works.
Intellisenses A special context-sensitive menu (the IntelliSense menu) appears when you start typing some things in the Code window. This menu shows the code that is available to you. It guesses what you need based on what you are doing. Assignment Statements Assignment statements in a procedure are used to change a value at run-time. One use of an assignment is to change the property of an object. To do this, the following syntax is used: Syntax: Assignment Statements ControlName.Property = value We will set the picture of the wizard to hide when the Hide button is clicked and show when the Show button is clicked. In the Designer view, double click on the Hide button. The Code view should open with the cmdhide_click procedure showing. In between the Private Sub and the End Sub Statements, type: picwizard.visible = False Run the program and click on the Exit button to ensure that this code works. From the drop-down menus, select cmdshow and the Click event. In between the Private Sub and the End Sub Statements, type: picwizard.visible = True Run the program and click on the Exit button to ensure that this code works. Note: if your program does not run, check on the names of your objects, ensuring that you have named them as you were asked to. Also, check that you have typed code properly in the proper locations. Add comments to your code.
Your program code should look something like the following: Note the yellow and green bar on the side of the code. The yellow bar is code that was added, but never debugged. The green bar indicates code that has been tested and works.