Docstoc

An introduction to Visual BASIC

Document Sample
An introduction to Visual BASIC Powered By Docstoc
					An Introduction to Visual BASIC                                    http://inqilab.co.cc




                          An Introduction to
                          Visual BASIC
                               By
                             Inqilab Ruknuddin Patel
                                      MCS




                                  RUKNUDDIN
                                  Insititute of Computer
                              Management & IT
                               http://ricmit.com
                             http://ruknuddin.com
                              http://inqilab.co.cc




RIC: CB-16, Gulistan Society Landhi Karachi 021-38570223, 021-35010293

Ruknuddin Institute of Computer             1              By Inqilab Ruknuddin Patel
An Introduction to Visual BASIC                                                                                http://inqilab.co.cc




Contents
Q 1) What is VisualBASIC? ........................................................................................................... 3
Q 2) What is Computer program? ................................................................................................... 3
Q 3) What is the programming language? ...................................................................................... 4
Q 4) Is VisualBASIC a smart language? ........................................................................................ 4
Why VB is popular language now a day? ....................................................................................... 4
Q 5) Write down the importance of designing program? ............................................................... 5
Q 6) What is a User Interface? ........................................................................................................ 5
Q 7) What is project and how it is created? .................................................................................... 5
Q 8) What is design mode and run mode? ...................................................................................... 5
Q 9) What are forms and what they do? ......................................................................................... 6
Q 10) What are Controls? ............................................................................................................... 6
Q 11) Write down component of VisualBASIC? ........................................................................... 6
Q 12) What is Variables and how it is declared? ............................................................................ 7
Q 13) Write a note of data type on use to VisualBASIC? .............................................................. 8
Q 14) Write a note on loops? ........................................................................................................ 10




Ruknuddin Institute of Computer                                     2                             By Inqilab Ruknuddin Patel
An Introduction to Visual BASIC                                        http://inqilab.co.cc



Q 1) What is VisualBASIC?
       VisualBASIC is a software development environment that enables programmers
to develop windows applications easily and quickly.

       VisualBASIC is a high level programming language so programmers can write
codes on English words.
       Visual refers to the way in which programmer develop applications GRAPHICAL
USER INTERFACE (GUI) (pronounciate as GOOEE) like buttons, text boxes,
scrollbars etc.
       BASIC refers to the way type of programming code that programmers write in
basic language (Beginners All-purpose Instructions Code).
       VisualBASIC provides following tools to create, modify, test, repair, and compile
a program.

a. Control Tools
       Controls tools are used to create GRAPHICAL USER INTERFACE (GUI) of
application like windows, buttons, text boxes, etc.

b. An Editor:
      Editor is used to write and edit programming code.

c. A Debugger
      Program errors are called bugs. A debugger is used to test the programs remove
programming errors.

d. A Compiler
      A compiler is used to convert source code into machine language object code.


Q 2) What is Computer program?
     A computer program is a set of instructions, that a computer follows to
accomplish specific task.
     A computer program is also known as application.

      There are two types of computer programs.

a. Package Program
      "A program, which is predesigned to accomplish some specific task is called
package program."
      For example Microsoft Word is used for word processing, Microsoft Excel for
spreadsheets, Microsoft Access for database management.

      A package program is available at any software store.
Ruknuddin Institute of Computer            3                  By Inqilab Ruknuddin Patel
An Introduction to Visual BASIC                                        http://inqilab.co.cc



b. Custom Programs
      "A program which is designed for a specific purpose within a particular
organization is called custom program."

       Custom programs are developed by a software house according to needs of
particular organization. Custom programs can be continuously modified as the
organization needs change.
       For example patient's record program of a hospital or payroll program of a
factory.


Q 3) What is the programming language?
       “A programming language is software which translates set of instructions
(programs) into the steps that a computer can understand and follow."

        Computer can understand only machine language which consists on binary digits
(0 and 1). Machine language is basically current signals ON and OFF (5 volt and 0 volt).
It means computer cannot understand set of instructions (programs) written in English.
It is a very difficult task for a programmer to write programs in machine language. A
programming language allows programmers to write instruction in something
resembling English. Then programming language converts these instructions into a
program containing machine language instructions that a computer can understand.

      Examples, FORTRAN (FORmula TRANslation), COBOL (Common Business
Oriented Language), Pascal, C, Basics (Beginners All-purpose Symbolic Instruction
Code), VisualBASIC, JAVA, HTML (Hyper Text Markup Language) are examples of
programming languages.


Q 4) Is VisualBASIC a smart language?

Why VB is popular language now a day?
VisualBASIC is a smart language;
       "VisualBASIC is called smart language because it makes writing a program very
easy through a graphical interface."

      Writing a computer program is a complex technology. In text based programming
system user interface is controlled through programming language commands.
VisualBASIC provides controls to design user interface easily and quickly.

       Controls are elements, used to add features to program without involving the
details of how these features work. It means programmer can concentrate on what he
wants program to do, not how to get programming language do it.


Ruknuddin Institute of Computer            4                  By Inqilab Ruknuddin Patel
An Introduction to Visual BASIC                                          http://inqilab.co.cc



Q 5) Write down the importance of designing program?
Importance of designing a program:
       It is critically important to design a program before beginning to code it. Programs
are structural in nature so a programmer should get into the habit of planning even with
small programs. He should sit down with the blank sheet of paper, make some notes
about what he wants the program to accomplish and sketch out what should the user
interface look like.


Q 6) What is a User Interface?
       "A program's User Interface is the part(s) of the program that the user sees and
interacts with."

        The VisualBASIC Integrated Development Environment (IDE) provides many
tools to develop applications user interface. The VisualBASIC integrated development
environment (IDE) makes short the work of interface design which was once a long and
typical task.


Q 7) What is project and how it is created?
Project:
     "A project is simply a set of files that store information about the component that
make up an application (program)."

Creating a project;
      To create a project VisualBASIC is started, the new project dialogue box enables
programmer to specify what type of project he wants to create. For examples; to
develop a calculator, standard.exe option is selected and Ok is pressed, and then
programmers enter in VisualBASIC environment.


Q 8) What is design mode and run mode?

Design Mode(Design Time);
       “When a programmer is developing his application, designing the interface or
writing the codes VisualBASIC is called in Design Mode."

Run Mode;
      “When a user is running VisualBASIC application VisualBASIC is called in Run
Mode."



Ruknuddin Institute of Computer             5                   By Inqilab Ruknuddin Patel
An Introduction to Visual BASIC                                          http://inqilab.co.cc


Q 9) What are forms and what they do?
Form:
       "A form is container that holds all the other controls ( such as labels, text boxes,
and pictures) that make up parts of program's user interface."

Parts of a Form
        A VisualBASIC form contains all the elements a programmer expects to find as
part of windows. Like:
        *     A title Bar
        *     A Control Menu
        *     A Set of Minimize, Maximize/Restore, and Close buttons.
        *     A Gird of Dots at Design Time to line up Controls easily.

Functions of Form
        Form represents program’s user interface which user sees and interacts with.
        Forms contain all control that user work with like, text boxes, command buttons
etc. Form acts as a sort of overall "Traffic Cop" overseeing the user of objects that make
u interface. Forms not only the objects the user can see but also program codes.


Q 10) What are Controls?
Controls;
      Controls are object designed to perform specific task. Controls have associated
properties, events and methods.
For examples, when using command buttons, programmer can set properties to
determine the size of button, font of caption and background colors etc.
      Programmer can integrate 3rd party controls in his VisualBASIC form or even
can create his own Active X controls to use in his form. Active X controls are
programmer's built controls.


Q 11) Write down component of VisualBASIC?
Component of VisualBASIC IDE;
       The VisualBASIC Integrated Development Environment (IDE) is made up of
following components to develop an application.
The components of VisualBASIC are given below:
1 Menu Bar
       The menu bar contains the commands which programmer needs in developing
VisualBASIC applications.

2 Tool Bar
      The tool bar contains icons of commonly used menu commands and provides
shortcut.


Ruknuddin Institute of Computer             6                   By Inqilab Ruknuddin Patel
An Introduction to Visual BASIC                                           http://inqilab.co.cc


3 Tool Box
       The tool box contains the icons of control which are used in creating user
interface. Such as buttons, text boxes, labels etc.
       By Default tool box contains one pointer and 20 controls.

4 Project Explorer
       The project explorer display list of files of applications such as project, forms and
class modules etc.

5 Properties Windows
      The properties window contains the property setting for the selected controls.
Properties are attributes of an object such as its size, caption and color.

6 The Form Designer Window
      The form designer window is the main window in the middle of screen. In this
window programmer designs and edits user interface. It also displays text editor and ts
code editer. The form designer window displays two windows.
      *       The form itself (the elements of visible user interface)
      *       A code Window (the code behind the elements of form).

7 Form Layout Window;
       The form layout window is used to determine the initial position of form in
application. This window is useful in applications that use multiple forms because
programmer can specify how each is positioned to main form.

8 The Immediate window;
        The immediate window is a debugging and while an application is running
programmer can step into the applications code change the values of variables or even
insert statements in code and then continue the execution.


Q 12) What is Variables and how it is declared?
Variables
       "Variables are placeholders where values are stored and recalled."
       In programming language variables store values during a program's execution.
For example the variable "Name" can have the value "SHOAIB" and the variable "Class"
can have the variable "DBA-1". In coding values of a variable are referred by the
variable "Name."

Declaring Variable
       In most programming languages variables must be declared (created) in advance
for the compiler. Because the complier must know all the variables and their types that
are going to be used in the application ahead of time it can produce the most compact
and efficient code.
       For example when programmer tells the compiler that variable "Name" will hold a
string the compiler will set a number of bits "Name" variable to use. There are two

Ruknuddin Institute of Computer              7                   By Inqilab Ruknuddin Patel
An Introduction to Visual BASIC                                        http://inqilab.co.cc


methods of variable declaration.
1 EXPLICIT DECLARATION
2 IMPLICIT DECLARATION.

1 EXPLICIT DECLARATION
      “When programmer himself creates a variable then it is called Explicit
Declaration."
      Usually programmer himself declares a variable.
Examples:
      Dim Name As String
      Dim Class As String
      Dim Fee As Currency

       When VisualBASIC finds "Dim" statement it creates variables by reserving some
space in the memory and assigning a name to it. In above examples VisualBASIC
creates variables "Name" of data type string "Class" of data type string and "Fee" of
data type currency.

2 IMPLICIT DECLARATION.
      "When VisualBASIC meets an undeclared variable, it creates a new variable on
the spot and uses it, this is called Implicit Declaration"
      The data type of implicit declared variables are Varient in VisualBASIC. Variant is
a generic data type that can accommodate all other data types.


Q 13) Write a note of data type on use to VisualBASIC?
Data Types (Variant Types)
           The following six types of data are used in VisualBASIC.
1 Numeric
2 String
3 Boolean
4 Date
5 Object
6 Variant.

1 Numeric Variables
       Numeric Variable stores numbers.
       A numeric data type may be of following variety:
   i. Integer
       Integer stores whole numbers in the range -32,767 to 32,767. It requires 2 bytes
       in memory.
   ii. Long
       Long stores whole numbers in the range about -2billion to 2billion it requires 4
       bytes in memory.


Ruknuddin Institute of Computer            8                   By Inqilab Ruknuddin Patel
An Introduction to Visual BASIC                                            http://inqilab.co.cc


   iii. Single
        Single stores number with decimal points (Floating point) in the range
        -3.40x10E38 to 1.4x10E-45. It requires 4 bytes in memory.
   iv. Double
        Double stores numbers with decimal points (Floating points) in the range of
        -1.79x10E308 to -1.40x10E10-324. It requires 8 bytes in memory.
   v. Currency
        Currency stores numbers with currency symbol and decimal points (Floating
        Points) in the range of + 9x10E19 to + 9x10E14. It requires 8 bytes in memory.
   vi. Byte
        Bytes stores whole numbers in the range of 0 to 255. It requires 1 byte in
        memory.

2 String
       The string data type stores only text string variables are declared with the string
type for example,
             Dim Name As String
       It requires 1 byte per character in memory. It can stores 2 billion characters.
String variables have following two variables:

   i. Variable Length String;
       String variable has variable length they grow, shrink according to data.
   ii. Fixed Length Strings
       It remains in the same size regardless of the information. If it assign information
       shorter then the define length the remaining length filed with space. It is explicitly
       declared, for example;
              Dim Name As String

3 Boolean
      The Boolean data type stores logical values "True" or "False". It requires 2 bytes
in memory. Boolean variables are declared as;
            Dim Paid As Boolean

4 Date
      Date variables stores date and time variables. It requires 8 bytes in memory.
      Date variable can be declared as;
            Dim DateofBirth As Date

5 Object
        An object variable refers to any VisualBASIC object. It is used to access objects.
It requires 4 bytes i n memory. It can be declared as following;
        Dim Save As Command button
        Dim Photo As Image.

6 Variant
      Variant is a flexible data type because it can contain all other types of data. A

Ruknuddin Institute of Computer              9                    By Inqilab Ruknuddin Patel
An Introduction to Visual BASIC                                            http://inqilab.co.cc


variable declared as variant or a variable which is not declared at all, is handled by
VisualBASIC according to variables current content. It is an integer value is assign to a
variant, VisualBASIC treat it as integer and if a string value is assign to a variant,
VisualBASIC treats it as a string. Variant variables are assign as follows,
               Dim Price
               Dim Price As Variant
or if a variable is assigned a value without declaration. It is declared implicit as variant,
modem speed = "100 hps"


Q 14) Write a note on loops?
Loop
       "A loop is group of instruction that executes repeatedly."
       When in a project a programmer wants to execute a instruction or a group of
instructions repeatedly, loops are used.
       VisualBASIC provides three types of loops:

   i. For - Next
   ii. While – Wend
   iii. Do-Loop

   i. For - Next
   A form next loop uses a numeric variable to control the number of operations this
   variable is called counter variable. For example to print natural number and its
   square from 1 to 10 following codes are used.

            Private Sub Square( )
                    Dim a As Integer
                    Dim s As integer
                    Print "Number", "Square"
                    Print "________" , "_______"
                    For a = 1 to 10
                            S=a*a
                            Print a,s
                    Next a
             End sub.
   ii. While - Wend
       The While - Wend loop executes a group of instructions while a condition is True.

      Syntax:
      While - Wend loop has following syntax:
            [While Condition
                   (Group of Instructions)
             Wend]
      If condition is true, all statements in statements are executed until the Wend
statement is encountered. Control then returns to the While statement and condition is

Ruknuddin Institute of Computer              10                   By Inqilab Ruknuddin Patel
An Introduction to Visual BASIC                                      http://inqilab.co.cc


again checked. If condition is still True, the process is repeated. If it is not true,
execution resumes with the statement following the Wend statement.




Ruknuddin Institute of Computer          11                  By Inqilab Ruknuddin Patel