Home

Introduction to Microsoft Visual Basic

 

A Project

 

Introduction

To create a computer program, also called an application, you create a series of files and group them in an ensemble called a project. This contains various modules, files, assemblies (or libraries), and resource files.

Creating a Project

A typical application consists of more than one module and can even be as complex as you want. To make it faster and a little easier to graphically create an application, you would need a good functioning environment like Microsoft Visual Basic. Using it, you can create a new project or you can open an existing one.

 

To create a Visual Basic project, you can display the New Project dialog box. To open the New Project dialog box:

  • On the Start Page, on the right side of Project, click Create...
  • If you are using Microsoft Visual Basic 2005 Express Edition, on the main menu, you can click File -> New Project... If you are using Microsoft Visual Basic 2005 Professional, on the main menu, you can click File -> New -> Project...
  • On the Standard toolbar, you can click the New Project button New Project
  • You can press Ctrl + Shift + N

In the New Project dialog box, select Visual Basic Projects, select the type of project, give it a name, specify its directory, and click OK.

Practical LearningPractical Learning: Creating a Project

  1. On the main menu, click File -> New Project or File -> New -> Project...
  2. In the Templates section, click Console Application
  3. In the Name edit box, type Exercise1
     
    New Project
  4. Accept the name in the  Location text box and click OK. This creates a new project

Compiling and Executing a Project

The instructions created for a Visual Basic project are written in plain English in a language easily recognizable to the human eye. After creating the file(s) of a project, you would compile the project to get an executable that becomes ready to be distributed to your users.

To compile and execute a project in one step, on the main menu, you can click Debug -> Start Without Debugging. Although there are other techniques or details in compiling (or debugging) and executing a project, for now, this is the only technique we will use until further notice.

Practical LearningPractical Learning: Executing a Project

  1. To execute the application, on the main menu, click Build -> Build Exercise1

  2. To execute the application, on the Standard toolbar, click the Start Debugging button Start Debugging

Opening a Project

As opposed to creating a new project, you can open a project that either you or someone else created. To open an existing project:

  • On the Start Page, on the right side of Project, click Open...
  • If you are using Microsoft Visual Basic 2005 Express Edition, on the main menu, you can click File -> Open Project... If you are using Microsoft Visual Basic 2005 Professional, on the main menu, you can click File -> Open -> Project...
  • You can press Ctrl + Shift + O

This action would display the Open Project dialog box. This allows you to select a project and open it.

Overview of GUI Applications

 

Introduction

Microsoft Visual Basic is a programming environment that allows you to create various types of applications. In our lessons, we will mostly create graphical applications, also called Windows applications or Windows Forms applications. 

A Windows application primarily appears as a rectangular object that occupies a portion of the screen. This type of object is under the management of the operating system, Microsoft Windows. Based on the functionality of Microsoft Windows, for an application to become useful, it must be opened. An application must have an entry point. On a C/C++ application, this entry point is a function called main. On a Win32 application, this entry point is a function called WinMain. In the Visual Basic language, this entry point is a function named Main.

Practical LearningPractical Learning: Starting a GUI Application

  1. To create a new application, on the main menu, click File -> New Project or File -> New -> Project...
  2. In the Project Types list, expand Visual Basic and click Windows.
    In the Templates section, click Empty Project
  3. In the Name edit box, type Exercise2 and click OK
  4. On the main menu, click Project -> Add New Item...
  5. In the Templates list, click Module
  6. Change the Name to Exercise
     
    Add New Item
  7. Click Add
  8. Change the contents of the file as follows:
     
    Module Exercise
    
        Function Main() As Integer
            Return 0
        End Function
    
    End Module

Windows Application Configuration

Although you can directly create a graphical application when starting your project, if you had created a console application, you can still easily transform it into a Forms application:

  • The most required action consists of changing some characteristics of the project. To take care of this, on the main menu, you can click Project -> ProjectName Properties... and then change the value of the Output Type combo box to Windows Application
  • Before or after setting the Output Type to Windows Application, you can create a form 

Practical LearningPractical Learning: Configuring a Windows Application

  1. On the main menu, click Project -> Exercise2 Properties...
  2. On the left side, make sure Application is selected.
    In the right section, click the arrow of the Output Type combo box and select Windows Application
     
    Properties
  3. Save and close the window

Forms Fundamentals

Windows Forms is a technique of creating computer applications based on the common language runtime (CLR). It offers a series of objects called Windows Controls or simply, controls. These controls are already created in the .NET Framework through various classes. Application programming consists of taking advantage of these controls and customizing them for a particular application. To exploit these controls and other features of the .NET Framework, there are various types of applications you can create, including graphical applications (Windows Application), web-based applications (ASP.NET Web Application), console applications (Console Application), etc.

The objects used in a Windows application are stored in libraries also called assemblies. As normal libraries, these assemblies have the extension .dll (which stands for dynamic link library). In order to use one of these objects, you must know the name of the assembly in which it is stored. Then you must add a reference to that assembly in your application.

To add a reference to an assembly, on the main menu, you can click Project -> Add Reference... You can also right-click the name of the project in the Solution Explorer and click Add Reference... Any of these actions would display the Add Reference dialog box from where you can click the reference, click Select and click OK. If you don't see the reference you are looking for, you can locate it on another drive or directory using the Browse button.

There are two broad categories of objects used in a Windows Forms application: the forms and the controls. A form is the most fundamental object used on an application. It is a rectangular object that uses part of the computer desktop to represent an application. A form is based on the Form class that is defined in the System.Windows.Forms namespace created in the System.Windows.Forms.dll assembly. Every GUI application you will create starts with a form. There are various techniques you can use to get a form in your application:

  • You can programmatically and manually create a form
  • You can inherit a form from the Form class
  • You can create a form based on another form that either you or someone else created already, etc.

The primary means of getting a form into an application consists of deriving one from the Form class.

Practical LearningPractical Learning: Deriving a Form From the Form Class

  1. To add a reference to the assembly in which the Form class is defined, on the main menu, click Project -> Add Reference...
  2. In the Add Reference dialog box, click the .NET tab if necessary and scroll down in the list
  3. Click System
  4. Press and hold Ctrl
  5. Click System.Windows.Forms
     
    Add Reference
  6. Click OK
  7. To inherit a form from the Form class, change the file as follows:
     
    Imports System.Windows.Forms
    
    Module Exercise
    
        Public Class Starter
            Inherits Form
    
        End Class
    
        Function Main() As Integer
            Return 0
        End Function
    
    End Module
  8. Save the file

The Application Class

The form is the object that gives presence to an application. Once you have created the (primary) form of your application, you can get it ready to display on the screen. This is taken care of by the Application class equipped to start an application, process its messages or other related issues, and stop the application.

The Application class provides the overloaded Run() method that can be used to start a program. One of the versions of this method takes a form as argument. This form must be the first, main or primary form of your application; it will be the first to display when the application comes up.

Practical LearningPractical Learning: Using the Application Class

  1. To prepare the application for starting, change the Main() method as follows:
     
    Imports System.Windows.Forms
    
    Module Exercise
    
        Public Class Starter
            Inherits Form
    
        End Class
    
        Function Main() As Integer
    
            'Instantiate an Program object
            Dim frmStart As Starter
    
            ' Allocate memory for the object, using the new operator
            frmStart = New Starter
    
            ' Call the Run() static method of the Application
            ' and pass it the instance of the class to display
            Application.Run(frmStart)
    
            Return 0
        End Function
    
    End Module
  2. Test the application
     
    Form
  3. Close it by clicking its system Close button and return to your programming environment

The Project Interface

 

Introduction

Besides the windows and functionalities we reviewed earlier, when you work on a project, there are other features that become available.

The Server Explorer

The Server Explorer is an accessory that allows you to access SQL Server databases without using the physical server and without opening Microsoft SQL Server:

Server Explorer

The items of this window display in a tree. To expand a node, you can click its + button. To collapse it, click its - button.

The Solution Explorer

The Solution Explorer is a window that displays the file names and other items used in your project:

Solution Explorer

The items of this window display in a tree. To expand a node, you can click its + button. To collapse it, click its - button. To explore an item, you can double-click it. The result depends on the item you double-clicked.

The Solution Explorer can be used to create a new class, a new folder, or a reference. To perform any of these operations, you can right-click a folder node such as the name of the project, position the mouse on Add and select the desired operation. You can also perform any of these operations from the Project category of the main menu.

Besides adding new items to the project, you can also use the Solution Explorer to build the project or change its properties. If you add one or more other project(s) to the current one, one of the projects must be set as the default. That project would be the first to come up when the user opens the application. By default, the first project created is set as the default. If you have more than one project, to set the default, right-click the name of the desired project in Solution Explorer and click Set As StartUp Project.

The Solution Explorer also you to rename or delete some of the items that belong to your project.

Practical LearningPractical Learning: Using the Solution Explorer

  1. To start a new project, on the main menu, click File -> New -> Project...
  2. In the Templates list, click Empty Project and change the Name to Exercise3
  3. Click OK
  4. On the main menu, click Project -> Exercise3 Properties...
  5. In the left frame, make sure Application is selected.
    In the right frame, click the arrow of the Output Type combo box and select Windows Application
  6. If the Solution Explorer is not visible, on the main menu, click View -> Solution Explorer.
    In the Solution Explorer, right-click Exercise3 and click Add Windows Form...
  7. In the Templates list, make sure Windows Form is selected.
    Set the Name to Exercise and click Add

The Class View

The Class View displays the various classes used by your project, including their ancestry. The items of the Class View an organized as a tree list with the name of the project on top:

Class View

The Class View shares some of its functionality with the Solution Explorer. This means that you can use it to build a project or to add new class.

While the Solution Explorer displays the items that are currently being used by your project, the Class View allows you to explore the classes used in your applications, including their dependencies. For example, sometimes you will be using a control of the of the .NET Framework and you may wonder from what class that control is derived. The Class View, rather than the Solution Explorer, can quickly provide this information. To find it out, expand the class by clicking its + button.

Practical LearningPractical Learning: Using the Class View

  1. If the Class View is not visible, on the main menu, click View -> Class View.
    In the Class View, expand the Exercise3 node if necessary.
    Right-click the name of the project Exercise3 -> Add -> Class...
  2. In the Templates list, make sure Class is selected. Change the Name to Central and click Add
 

Previous Copyright © 2008 FunctionX, Inc. Next