Wal Mart Online → Best Selection
WM468X60_ko_static.gif

Last Modified:
9-15-2010

Home


Visual Basic

Introduction to VB.NET

.NET Framework

VS2008 IDE

How VB is Compiled

Start Visual Studio


Windows Form App


Save Your Work

VB OOP Programming

Visual Basic Code


Exit Code


Button Event Code

Coding Recommendations

If/Then/Else

Error List Window

Comment Syntax

Help Window

Language Essentianl

Built-In Data Types


Declare Variables

Declare Constants

Code Arithmetic Expressions

Assignment Statements

Operator Precedence

Type Casting


Math Class

String Declaration

Conversion Functions

Conversion Methods

Formatting Functions

String Formatting


Variable Scope

Enumerations

Nullable Types


Loop Constructs

For Next Loop

Do While Loop


Do Until Loop

Do...Loop-While

Do...Loop-Until


Exit Do | Exit For

Do...Loop


Nested Loops


Arrays

Array Declaration


Rnd( ) Function

Listbox Control


KeyPressEventArgs


Parallel Arrays

Key Event Args


Dynamic Arrays

Redimension Array

Set Breakpoint

Start Debugger


ReDim Preserve


MultiDimensional Arrays

DataGridView Control


Length and Sort Methods


Structures

Pad Right

Split Method

IsNumeric Function


Multiform Projects


Add Form To Project

Form Object Methods

Form Show Method

ShowDialog Method

Form Close Method

Form Accept Button

Multiform Project Example


ASP.NET Web Programming

Create Data Source


Configure Access Data Source


Add Product Class


Extract Local Database Data

Order PageLoad VB Code

Add New Web Page


Set Start Page

Display Cart Aspx Code

Display Cart Design View

Sorted List Definition

VB.NET Session State


Create CartItem Class

GetCartContents Function


Add To Cart Event Handler

Remove Cart Item Event


Clear Cart Event Handler



Visual Basic Exit Program Event Code

Double-Clicking the Exit Button (Figure VB-24) will bring up the Source code window. All the code referencing the InvoiceTotal Form is contained in the Source Code Window. The following diagram shows the contents of the Source Code Window:

Exit Button Code Template

Figure VB-25: Visual Basic Source Code Template for Exit Button


Place the cursor between Private Sub btnExit_Click and End Sub at the point where Figure VB-25 (above) says: Enter Code Here by clicking the mouse pointer at that location. Enter the letters: 'me' at that location. This will bring up the Microsoft Visual Studio completion list as indicated in the diagram below:

Completion List Me

Figure VB-26: Visual Basic Completion List - Me Highlighted


When you enter the letters: "me" the completion list may highlight the word Me which is the word we wish to enter at this point. Visual Studio attempts to guess at the word you need next based on your previous programming techniques as well as the most likely response under the current situation. If Me is not highlighted, you have two choices:

  • Press the up/down arrows until the word Me is highlighted.
    • The completion list is in alphabetical order, so Me will be the first "Me" entry (up arrow).
  • Press the '.' indicating that we have completed the Me class entry, and are ready to enter the Close Method name.

It is best to use the completion list whenever possible because it will reduce syntax errors and help make the proper Object Oriented programming statement for the task you are trying to accomplish. This is very important in an almost 4G level language like the .NET Framework.

In Visual Basic, when referring to the current Form, it is customary to use the keyword Me to identify the Form. The task we wish to accomplish with the Exit Button is to close the current Form and terminate the program. The one-line statement in Visual Basic that will accomplish this task is:

Me.Close()

The Exit function when completed should look like the following diagram:

Me Dot Close

Figure VB-27: Completed Exit Function using Me.Close()


I don't know about you, but I am chomping at the bit to try out this Visual Basic Object Oriented Program with Class functionality on a Visual Studio .NET Framework software development project. Whew, that was a mouthful. I would personally rather just say I hacked a couple of lines of Visual Basic code, but that might not impress some prospective employer, so we'll go either way, depending on who we are talking to at the moment. Press the green triangle Start Debugging Symbol to start the program execution with some functional Visual Basic code and see what we get. But this time, let's press the Exit Button, , we placed on the Form instead of the Red X Red X to terminate the program:

Looks like it works just like those clever software developers @ Microsoft planned. The <esc> key seems to work just fine too. Remember, we told .NET with the Form Property → CancelButton → btnExit in Figure VB-17 that the cancel or esc key was to act as a button press on the button?

We are now going to move towards obtaining functionality with the button. Putting in error checking and validation at this point, just makes it more difficult to get a clear understanding of some of the underlying concepts, so we are going to hold off on that for a moment, and just manually enter data in the correct format into the Subtotal TextBox:

Subtotal:


When we press the calculate button we want the following events to occur:

  • Pull the data out of the Subtotal TextBox and get the numeric equivalent.
    • Subtotal:
  • Set a default value of 10% for the DiscountPercent
    • DiscountPercent=.1;
  • Mulitply the Subtotal by the DiscountPercent to get a DiscountAmount.
    • DiscountAmount = Subtotal * DiscountPercent;
  • Display the Value of 10% in the DiscountPercent TextBox (txtDiscountPercent).
    • Discount Percent:
  • Display the DiscountAmount in the DiscountAmount TextBox (txtDiscountAmount)
    • Discount Amount:

Press the Form1.cs[Design] tab to bring up the graphical view of the Design Form. Place the mouse pointer over the button and double-click as illustrated in the diagram below:

Double-Click Calculate Button

Figure VB-28: Select Design Tab and Double-Click the Calculate Button


This will bring up the source code window, and creating the Calculate Button Click-Event template. Enter the code as indicated in the diagram below, using the code completion list as much as possible to reduce programmer errors:

Calculate Code 1

Figure VB-29: Visual Basic Source Code for Invoice Total Calculate Button


Let's take a quick look at the statements in this Sub:

  • Private Sub btnCalcuate_Click: Event generated by .NET Framework to respond to Mouse-Click or <ENTER> key.

  • Dim Subtotal As Double = CDbl(txtSubtotal.Text)
    • Dim: Declares and allocates temporary storage space for a variable.
    • Subtotal Name of Variable we are creating in this statement.
    • As Double: The data type that Subtotal acquires the characteristics of.
    • CDbl: Visual Basic Convert to Double built-in function.
      • Will convert the data inside the parantheses to the Double datatype.
    • (txtSubtotal.text): This refers to the data entered in the TextBox field by the User.

  • Dim DiscountPercent As Double = 0.1 Variable we are using to keep track of the discount on the Subtotal.
    • We are setting the default value to 10% for this example.

  • Dim DiscountAmount As Double = Subtotal * DiscountPercent; Declaration of variable DiscountAmount
    • Calculated as the Subtotal multiplied by the DiscountPercent amount.

  • txtDiscountPercent.Text = DiscountPercent.ToString("p1"); Updates the Discount Percent TextBox on the Form.
    • txtDiscountPercent.Text=: Refers to the text the user sees in the Discount Percent TextBox.
    • DiscountPercent: This is the numeric value that will be displayed in the TextBox.
    • .ToString: .NET Framework Method to convert Numeric values into a text representation.
    • ("p1"): Instructs the .NET Framework to make a Percent (the p) with 1 place to the right of the decimal.

  • txtDiscountAmount.Text = DiscountAmount.ToString("c"); Updates the Discount Amount TextBox on the Form.
    • txtDiscountAmount.Text=: Refers to the text the user sees in the Discount Amount TextBox.
    • DiscountAmount: This is the numeric value that will be displayed in the Textbox.
    • .ToString: .NET Framework Method to convert Numeric values into a text representation.
    • ("c"): Instructs the .NET Framework to make a Currency('$') value with 2 places to the right of the decimal.

We are now going to test our code by following the steps below:

  • Press the Start Debugging Start Debugging Symbol symbol.

  • Enter the value 100 in the Subtotal TextBox: Subtotal TextBox

  • Press the <ENTER> key on your keyboard or the Calculate Button button.

Performing the above steps should result in program output as indicated in the diagram below:

Invoice Total Runtime1

Figure VB-30: Visual Basic Program Invoice Total At Runtime


Now that we have some familiarity with integrating Visual Basic code with .NET Framwork Forms, we are ready to expand the requirements of pressing the Calculate Button button. In order to view the source code for the Calculate Button button and continue this guide on Visual Basic press the Button Below:



Help Support this site


Thank you


Work Independently
Full/Part Time