Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

University of Basrah

Collage of Engineering
Department of Petroleum
Computer Programming

Introduction to Visual Basic Express 2010

First Year

By

Assist Lect. Haider S. Mohammed


Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

1.1 What is visual basic 2010?

Visual Basic 2010 is the latest version of Visual Basic launched by Microsoft in 2010. Which evolved
from the earlier DOS version called BASIC. A graphical-based language allows the user to work
directly with graphics. Visual Basic is derived from the “visual” term refers to the method used to
create the graphical user interface (GUI). The “Basic” term refers to the BASIC (Beginners All-
Purpose Symbolic Instruction Code) language, a language used by more programmers.

 Getting started with Visual basic 2010

To run Visual Basic program, select:

1. Start menu >> Programs >> Microsoft Visual Studio 2010 Express >> Microsoft Visual Basic 2010
Express
2. New Project
3. Windows Forms Application
4. The main window of the program

1
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

1.2 Elements of Integrated Development Environmental (IDE).

The IDE contains several components as shown below:

Menu Bar
Tool Bar

Solution Explorer

Form Window

Toolbox

Error List Properties Window

 Menu Bar: Contains a standard command and specific command like (File, Edit, View, Project,
Format, Debug, Run, etc.).
 Tool Bar: Contains several icons that provide quick access to commonly used features.
 Error List windows: display the errors that could be occurred in the program
 Solution Explorer: The Solution Explorer contains a list of files and forms in your project. If
the Solution Explorer isn’t visible, go the View menu and select Solution Explorer to display it.
 Form1 (Form) window: contains a form named Form1, which is where the program’s Graphical
User Interface (GUI) will be displayed. A GUI is the visual portion of the program, this is where
the user enters data (called inputs) to the program and where the program displays its results
(called outputs). We refer to the Form1 window simply as “the form”. Forms are the foundation
for creating the interface of an application. You can use the forms to add windows and dialog
boxes to your application. You can also use them as containers for items that are not a visible part
of the application’s interface. For example, you might have a form in your application that serves
as a container for graphics that you plan to display in other forms.

2
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

 Toolbox controls

The Toolbox contains the controls you need to design a form. You can click on a control in the
Toolbox and drag it to the form to make it a part of the user interface. Each control in the Toolbox
has a special use, just like the tools in real toolbox. You’ll soon learn how to use some of the basic
tools in the Toolbox.

 Properties Window

The Properties window contains a list of properties for each control. When you select the form or any
control on a form, the Properties window displays the properties for that control. The column on the
left contains the names of the properties for that control. The column on the right contains the settings
for the properties. You can change these settings, and thus change your program, by clicking on the
name of the property and changing its setting on the right. Each control has its own unique set of
properties. Most controls have similar properties so be careful to select the correct control on the form
before you make changes to any properties. Table (2) explain objective of the properties window.

2 Saving and Running a Project


 To save a project, use the File - Save All menu. You can click the Save All button shown in the
figure below. This will save all files in the project. Also, you can change the name of the project
as shown below after that click save.

3
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

 There are several ways to run a project to test it.

Use the Debug menu, Start Debugging option, or Press the F5 function key to run the project, or
Click the shortcut green arrow on the shortcut toolbar.

Example 1: Design a form with one text box and two Commands button. Write a code so when run
project and click on Button1 (O.k.) display the word (Welcome) in text box, and when click on
Button2 (Close) terminate the program and return back to the form interface.

4
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

Solution:

Creating the Interface: The first step in building a Visual Basic application is to create the forms
that will be the basis for your application’s interface. Then you draw the objects that make up the
interface on the forms you create.
1. Adding a text box to the form. Double-click the toolbox’s textbox to create
a text box with sizing handles in the center of the form.
2. Adding a Button1 to the form. Click on button and draw button1 to form then the button appears
on form.
3. Repeat step 2 to add a Button2 to the form.

Setting Properties
The next step is to set properties for the objects. The properties window provides an easy way to set
properties for all objects on a form. For Example1, you’ll need to change three property settings. Use
the default setting for all other properties.
Object Property Setting
Name Form1
Form1 Text Example1
Font Bold and size 12
Name Button1
Button1 Text O.k
Font Bold and size 12
Name Button 2
Button2
Text Close
Font Bold and size 12
Name Textbox1
TextBox
Text Empty
5
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

Writing Code:
The code editor window is where you write Visual Basic code for your application. Code consists of
language statements, constants, and declarations. To open the code window, double-click the form or
control for which you choose to write code, or from the Project Explorer window, select the name of
a form and choose the View code button.
 In the Object list box, select the name of an object in the active form. Or double click of an
object.
 In the procedure list box, select the name of an event for the selected object. The Click
procedure is the default procedure for a command button and the Load is default procedure
for a form.
 An event procedure for a control combines the control’s actual name (specified in the name
property), an underscore ( _ ), and the event name. For example, (Button1_click).
 Type the code between the Private Sub and the End Sub statements.

Choose Button1 and type the following code:


Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
TextBox1.Text = ("welcome")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


End
End Sub
End Class

Running the Application


To run the application, choose start from the run menu, or click the start button on the toolbar, or F5
Click button1 (O.k.) and see the “Welcome” displayed in the text box. Click button2 (close) the end
the program and return to the form window.

Example 2: Design a form shown in figure below, with three Text boxes and Button. Write code in
the Button1 (Execute). So when run project enter the Student Name in TextBox (TextBox1) and the
Father Name in TextBox (TextBox2). When click on Button1 (Execute) display the Full Name in the
TextBox (TextBox3).

6
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

Solution:
Creating the Interface.:
1. Adding a Label to the form1. Double-click the Label’s Label to create a Label with sizing
handles in the center of the form1.
2. Repeat step 1 to add Label2 and Label3.
3. Adding a Textbox to the form1. Double-click the toolbox’s textbox to create a text box with
sizing handles in the center of the form1.
4. Repeat step 3 to add TextBox2 and Textbox3.
5. Adding a Button1 to the form. Click on button and draw Button to form then the Button1 appears
on form1.

7
Computer Programming / VB2010 Assist Lect. Haider S. Mohammed

Setting Properties
Object Property Setting
Name Form1
Form1 Text Example2
Font Bold and size 12
Name Button1
Button1 Text Execute
Font Bold and size 12
Name Textbox1
TextBox1
Text Empty
Name Textbox2
TextBox2
Text Empty
Name Textbox3
TextBox3
Text Empty
Name Label1
Label1 Text Student name
Font Bold and size 12
Name Label1
Label2 Text Student name
Font Bold and size 12
Name Label1
Label3 Text Father name
Font Bold and size 12

Writing Code:
Private Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
TextBox3.Text = TextBox1.Text + " " + TextBox2.Text
End Sub

Running the Application


To run the application, choose start from the run menu, or click the start button on
the toolbar, or F5 Click the command button1 (Execute) and see the Full Name
displayed in the TextBox3.

You might also like