CSC 314: VISUAL BASIC-Week 1
Mr. Salamudeen Alhassan Department of Computer Sc. UDS, Navrongo, Ghana.
Course Outline
Introduction VB controls Writing Program Code Variables; Constants VB Programming Fundamentals Writing Statements; Using Assignment Statements Using Mathematical operations Working with strings Formatting results Control structures; Making decisions, If statements; Select Case Statements Working with loops; Counter or for loops; Conditional loops; enumerator loops Procedures and Functions Arrays Database (linking DB to VB program) OOP(classes, inheritance, polymorphism etc.)
Introduction
Visual Basic 2008 is one of the latest versions of Visual Basic launched by Microsoft in 2008. The latest version is Visual Basic 2010. Visual Basic has gone through many phases of development since the days of BASIC that was built for DOS. BASIC stands for Beginners' All-purpose Symbolic Instruction Code. The program code in Visual Basic resembles the English language. Different software companies had produced many different versions of BASIC for DOS, such as Microsoft QBASIC, QUICKBASIC, GWBASIC, and IBM BASICA and more.
Introduction Cont.
Microsoft launched the first graphical BASIC, Visual Basic Version 1 in 1991. It is GUI based and especially developed for MS window. Since then Microsoft slowly phased out the DOS versions of BASIC and completely replaced them by Visual Basic. Visual Basic was initially a functional or procedural programming language until the popular Visual Basic 6. Then, Microsoft transformed Visual Basic into a more powerful object oriented programming language by launching Visual Basic.Net, Visual Basic 2005, Visual Basic 2008 and the latest Visual Basic 2010.
Introduction Contd
VB2008 is almost similar to Visual Basic 2005 but it has added many new features. Visual Basic 2008 is a full fledged ObjectOriented Programming(OOP) Language, so it has caught up with other OOP languages such as C++, Java, C# and others.
If you are familiar with Visual Basic 6, you can learn VB2008 effortlessly because the syntax and interface are similar.
The IDE
Consist of a few panes, namely;
The Recent Projects Pane- it shows the list of projects that have been created by you recently.
The Getting Started Pane- It provides some helpful tips to quickly develop your applications.
The VB Express Headlines pane- It provides latest online news about Visual Basic 2008 Express. It will announce new releases and updates
The if-else construct
The basic form of if-else is given by: if condition
statementsA
else statementsB end
Creating a New Project
Click on File and select New Project The New Project Dialog box appears. The dialog box offers you some types of project to create. As we are going to learn to create windows Applications, we will select Windows Forms Application. At the bottom of this dialog box, you can change the default project name WindowsApplication1 to some other name you like, for example, MyFirstProgram.
After you have renamed the project, click OK to continue. The following IDE Windows will appear, it is almost similar to Visual Basic 6. It consists of an empty form, the common controls toolbox, the solution explorer and the properties.
Creating a New Project Contd
Create your first program. First of all, drag one common button into the form and change its default name to calculate. Next, click on the calculate button and key in the following code at the source code window as shown below.
Dim num1, num2, sum As Single num1 = 150 num2 = 230 sum = num1 + num2 MsgBox(" The Sum of " & num1 & " and " & num2 & " is " & sum)
Run your program.
VB Controls
Controls in VB are useful tools that can be placed in the form to perform various tasks. They are used to create many kinds of Windows applications. The diagram on the right is the Toolbox that contains the controls of VB2008. They are categorized into Common Controls, Containers, Menus, Toolbars, Data, Components, Printings and Dialogs.
10
VB Controls Contd
Some of the most used common controls are Button, Label, ComboBox, ListBox, PictureBox, TextBox etc. To insert a control into your form, you just need to drag the control and drop it into the form. You can reposition and resize it as you like. Lets examine a few programs that made use of Button, Label, TextBox , ListBox and PictureBox .
11
VB Controls Contd
Using TextBox A multiplication program
Insert two textboxes , three labels and one button. The two textboxes are for the users to enter two numbers, one label is to display the multiplication operator and the other label is to display the equal sign. The last label is to display the answer.
The code
Dim num1, num2, results As Single num1 = TextBox1.Text num2 = TextBox2.Text results= num1 * num2 Label3.Text = results
12
VB Controls Contd
Using the ListBox A program to add items to a list box
This program will add one item at a time as the user enter an item into the TextBox and click the Add button. It consist of a label, a TextBox, a ListBox and a button controls. The Code Dim item As String item = TextBox1.Text ListBox1.Items.Add(item) To add item to listbox
13
VB Controls Contd
Using a PictureBox
In this program, we insert a PictureBox and a Button into the form. Set the SizeMode property of the PictureBox to StretchImage so that the whole picture can be viewed. The Code To load an image into the PictureBox from an image file PictureBox1.Image = Image.FromFile("C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg")
14
VB Control Properties
Before writing an event procedure for the control to response to a user's input, you have to set certain properties for the control to determine its appearance and how it will work with the event procedure. You can set the properties of the controls in the properties window at design time or at runtime. Setting caption, backcolor, forecolor, and others
15