vb programming PDF

Title vb programming
Author ones kim
Course Information communication technology
Institution Sang'alo Institute of Science and Technology
Pages 74
File Size 1 MB
File Type PDF
Total Downloads 50
Total Views 161

Summary

vb programming...


Description

GANANA INSTITUTE OF MEDICAL SCIENCES AND TECHNOLOGY

GANANA INSTITUTE OF MEDICAL AND HEALTH SCIENCES

Visual Basic Programming Language

Prepared By MR.ONESMUS

1

@computer Studies Department

GANANA INSTITUTE OF MEDICAL SCIENCES AND TECHNOLOGY

Programming in visual basic It is a window based programming language that evolved from DOS based BASIC programming language. Visual refers to the method used to create the graphical user interface (GUI). BASIC is the abbreviation for Beginners All purpose Symbolic Instruction Code. Visual basic has evolved from the original BASIC language and now contains several hundreds statements, functions and keywords many of which relate directly to the windows (GUI). VB provides an integrated development environment (IDE) in which you can develop, run, test and debug your applications. It also integrates many different functions such as design, editing, compiling and debugging within a common environment. Visual basic is also an object oriented programming language i.e. it revolves around ready made objects and it‘s also an event driven i.e. all the activities in a program are triggered by one event or another. Each object has its own properties determining its position, size, color, appearance and nature of its text and much more. Each object has also its own event handling procedures. The programmers‘ job is to determine where, how and when an object appears in the screen, what its caption reads and what happens when an event occurs. Visual basic supports application programming interface (API). API is the library of shared code that all windows application use for tasks like creating and displaying, windows, drawing text and graphics on the screen, tracking mouse clicks and other tasks. In most case, the user has no need to worry about API functions. You just have to click and drag controls and write basic codes. Visual basic does the work of translating these elements into API functions behind the scenes.

VISUAL BASIC DEVELOPMENT ENVIRONMENT Using VB one can easily design the visual elements of a new programming project. The following types of applications can be created using visual basic: I. Standard exe Standard exe project I a typical application which contains a form by default. Use this project template to develop a stand-alone application. II. Data project This project contains a data environment and a data by default use this project template to develop an application that reads or manipulates data from a data source. III. Active X EXE /Active X DLL These projects are available with the professional edition of vb. These project templates are used to develop components that expose functionality to other applications .use an active x exe projects if your components will both explore functionality programmatically and run as stand alone application and use an an active x DLL project templates if your component will only be used programmatically by another application IV. ACTIVE X CONTROL This project type is also packaged with the professional edition of vb.this template is used to create active x controls use this project template to create a component design to use interface element in a form or dialogue box V. ACTIVE X DOCUMENT EXE /ACTIVE X DOCUMENT DLL Active X documents are visual basic applications that can run in the environment of a container that support hyper linking. Use these project templates to create components designer for use in a document object container such a internet explorer. VI. DHTML APPLICATION These applications are a combination of compiled visual basic and dynamic Html. Use this project template to create a component that can be used on the client side of a web application. Prepared By MR.ONESMUS

2

@computer Studies Department

GANANA INSTITUTE OF MEDICAL SCIENCES AND TECHNOLOGY VII. IIS APPLICATION This is a server based project that derives html and other internet contents to client browsers. use this project template to create a component that can be used on a server side of a web application

VISUAL BASIC IDE ELEMENTS 1. The menu bar It presents visual basic menu which contains commands which you use to work with. These include: a) File – file handling and printing also used to make EXE files b) Edit – standard editing functions c) View –displays or hides windows and toolbars d) Project – sets projects properties, adds or removes forms and modules and adds or removes reference and components e) Format – aligns or sizes controls f) Debug- starts or stop debugging and stepping through programs g) Run- starts a program or compiles and starts it h) Tools- adds procedures, starts the menu editor and sets IDE options i) Add-ins – adds in manager, lists, add-ins like application wizards and API viewer window j) Window – arranges or select open windows k) Help –it handles help and the about box 2. Tool bar It contains buttons matching popular menu items. it provides quick access to commonly used commands in the programming environment. 3. The project explorer It‘s the window that allows you to co-ordinate the parts of your program into folders for easy manipulation. It lists the forms and modules in your current project. A project is a collection of files you are to build an application. To pick out the part of the project you want to work on you only have to find it in the project explorer and double click it. You can also add or remove items by right clicking them with the mouse in the project explorer. The buttons at the top of the project explorer allows you to switch between views. 4. The properties window. It‘s a window used to set properties of various objects. When you select an object like a control in the VB using the mouse, that object properties appear in the properties window. To change or examine a property setting in the properties window you just find that property‘s item in the window. 5. Object browser It provides you with an overview of all the objects in a project, including their properties and methods and what parameters to pass. You can see the object browser to explore objects in VB and other applications 6. The tool bar It provides asset of tools that you can use at design time to place controls on a form. You do this by just clicking a tool and draw the new button in a form 7. The form layout window It‘s used to position forms as you want them to appear in the screen when they are first displayed. This is done by dragging that form in the form layout window to that new location. 8. Form designers’ windows. Prepared By MR.ONESMUS

3

@computer Studies Department

GANANA INSTITUTE OF MEDICAL SCIENCES AND TECHNOLOGY The form designer displays the current form under design. It serves as a window that you customize. To design the interface of your application, you add controls, graphics and pictures to a form to create the look you want. 9. Code editor window It serves as an editor for entering application code. A separate code editor window is created for each module in your application. To open an objects code in the code window, just double click that object or right click then close code editor. There are two drop-down list boxes at the top of the code window ;the left lists lets you select the object to add code to, and the right lists lets you select the p[procedure to add{all the methods the object supports appear in this list. 10. Context menus It contains short cuts to frequently performed actions. To open a context menu, click the right mouse button on the object you are using. STAGES IN APPLICATION DEVELOPMENT Program statements in an event driven application execute only when a specific event calls a section of a code assigned to that event. Creating event driven programs in VB requires an approach different from that used in procedural languages. Creating an application involves each of the following steps i. Create a design plan The design plan should be the road map you use when creating an application. This planning will help you to understand user‘s needs and be more efficient in your development efforts. ii. Create the user interface To create the interface for your application, first place controls and objects on a form by drawing or painting in the form designer then position the controls and objects according to your need iii. Set the properties of the interface Once you have added objects to a form, you can set their properties at design time or you use code statements to set properties at run time iv. Write code for events After you have set the initial properties for the form and its objects, you can add code that runs in response to events. Events occur when different actions are performed on a control or object v. Save the project Save the project giving it a unique and description name. You should save the project frequently as you add code to it vi. Test and debug the application As you add code to the project, you can run the application and view its behaviour .VB also offers numerous tools for debugging your application. vii. Make an executable file Upon the completion of the project you will create an executable file. This combines the various files making up the program into a stand-alone executable file. viii. Create a set up application In order to run your application, the user usually needs other files such as DLL files or custom controls OCX files you used to create your application. VB provides the package and deployment wizard which automates the creation of the set up program and ensures the user has all necessary files. VISUAL BASIC TERMINOLOGIES Some of the common terms used in development environment are; Prepared By MR.ONESMUS

4

@computer Studies Department

GANANA INSTITUTE OF MEDICAL SCIENCES AND TECHNOLOGY  Forms It‘s a window that can be customized to serve as the interface for an application or a dialogue box used to gather information to the application.  Controls Its graphic representation of objects such as buttons, list boxes used to manipulate to provide information to the application  Properties It‘s data that describes an object. They are characteristics of an object such as color, caption or size.  Objects It‘s a general term used to describe all forms or controls that make up a program.VB objects have properties, methods and events.  Methods. It‘s the actions that an object can perform or that can be performed on the object i.e. the things you can tell the object to do  Events Its actions recognized by a form or control. Events occur as the use of the operating system or application interacts with the objects of a program i.e. events are the things the object does  Design time This describes any time an application is being developed in the VB environment. At this time controls are added to the form and the code for the different events the control will respond to  Run time Its any time the application is running VISUAL BASIC FILE TYPES A VB project is made up of the forms, modules, active X controls and environment settings that an application requires .VB maintains a project file with extension vbp .In addition to project file, a project can include the following files; 1) Form files These files contain the form, the objects on the form and code that runs when an event occurs on that form. The file extension FRM or FRX 2) VB standard module extension (BAS) These modules contains sub and function procedures that can be called by any form or object on a form. Standard modules are optional 3) Active X controls file extension( OCX) Active x controls are available from Microsoft and third party vendors. They are added using the components command on the project menu 4) VB class modules extensions (CLS) The modules contains the class definition methods and properties class modules are optional 5) Resource files extensions (RES) This file contains binary information used by the application. Resource files are typically used when creating programs for multiple languages. 6) User control extensions( CFL and CFX) These files contain source code and binary information for use in controls. These files are compiled into Active X Controls (.OCX files) 7) Upper document extension (DOBand DOX) These files contain base form and binary information for creating Active X Documents 8) Active designers extensions These files contain information about designers that are used in the project.e.g you can create a data environment for run-time data access Prepared By MR.ONESMUS

5

@computer Studies Department

GANANA INSTITUTE OF MEDICAL SCIENCES AND TECHNOLOGY DATA TYPES (VARIABLE TYPES) Variables in VB are declared using Dim statements. Though you don‘t have to declare variable before you first use them, its good practice to declare variables as this helps to prevent errors and allow your computer to work more efficiently. The rules for variable names are; a) They must begin with a letter, b) They may contain any mixture of letters and numbers, c) They may not include punctuation or symbols, with the exception of under score, d) They may not have more than forty characters. VB supports a wider range of data types (variable types) than is found on other Basics. These include;  Integer This is a whole number in the range of -32,768 to +32767  Long This is a whole number in the range of -2,147,483,648 to 2,147,483,647  Single These are floating data type which holds positive or negative numbers up to around 3.4E38  Double It‘s a floating data type which holds positive and negative numbers to around 1.8E308  Currency This data type is called scaled integer because it deals with a fixed point numbers that have 5 digits to the left of the decimal and 4 digits to the right. This is more accurate than floating point numbers for currency values.  Date It holds dates for january1, 100 to december31, 9999.Time information is also stored.  Byte This data type holds positive numbers from 0 to 255  Boolean This is a simple data type that holds two values either True or False  String This is a sequence of up to two billion characters  Variant It‘s a highly flexible form of storage that takes string or number values. The nature of the variable can change during the programs run.  Object The object data type stores VB objects. These can also be automation object from other applications CONVERTING BETWEEN DATA TYPES. VB supports a number of ways of converting from one type of variable to another. The following table illustrates conversion statements To do this,  ANSI value to string  String to lower case or upper case  Date to serial number  Decimal number to other bases  Number to string  Date to day, month, weekday or year  Time to hour, minute, or second Prepared By MR.ONESMUS

6

Use this chr Format, L case, U case Dateserial, datevalue Hex, Oct Format, str Day, month, weekday ,year Hour, minute, second @computer Studies Department

GANANA INSTITUTE OF MEDICAL SCIENCES AND TECHNOLOGY    

String to ASCII value String to number Time to serial number One data type to another

ASC Val Time serial, time value CBOOL,BYTE, CCUR, CDATE, CDBI, CDEC, CINT, CLNG, CSNG, CSTR, CVAR, CRERR, FIX,INT.

. SETTING VARIABLE SCOPE There are three levels of variable scope in VB as follows; a) Variables declared in the procedures as private to the procedure b) Variables declared at the form or module level in the form or modules (general) section using Dim, ReDim,Private, Static or Type area form or module-level variables. These variables are available through out the module. c) Variables declared at the module level in the module‘s(general)section using public as global and are available through out the project in all forms and modules .NB; you cannot use PUBLIC in procedures. HANDLING STRINGS There are two types of strings available in VB namely;  Variable length strings,  Fixed length strings Variable length strings can contain up to approximately two billion characters and can grow or shrink to match the data you place to it e.g. Dim Named as String Fixed length string as declared with an asterisk character (*) followed by the strings ‗length eg. Dim Address as String*20 The address will contain 20 characters. The table below gives a list of string functions; To do this  Compare two strings  Convert to lower case or upper case  Find length of string  Justify a string  Manipulate strings

Use this Str comp Lcase, Ucase Len Lset, Rset Instr, left, LTRIM, MID, right, RTRIM, TRIM

DATA TYPES  Declaring constants You declare constants in VB with the CONST statement; {PUBLIC/PRIVATE} const constname [AS TYPE]=EXPRESSION The public keyword is used at the module level to make a constant global. This keyword is not allowed in procedures. The private keyword is used at module or form level to declare constants that are private which means only available within the module or form where the declaration is made. Private is not allowed in procedures The constant name identifier is the actual name of the constant. Type identifier is the data type which might be Boolean, byte, integer long, currency, single, double, date, string, or variant. Prepared By MR.ONESMUS @computer Studies Department 7

GANANA INSTITUTE OF MEDICAL SCIENCES AND TECHNOLOGY The expression identifier holds the value you want for this constant. Example, Private Sub COMMAND-Click ( ) CONST PI=3.14159  Declaring variables Before using variables, you have to set a side memory space for item. Usually you use the Dim statement to declare variables although you can use PRIVATE, PUBLIC, STATIC (Declaring a variable that holds its value between procedure calls) REDIM (Redimension a dynamic away) or TYPE (Declare a user defined type) key words to declare variables. THE DIM STATEMENT Dim {with events} varname{([subscripts] )] [AS[NEW]type]C. [WITH EVENTS]varname [([Subscripts])] [AS[NEW]type] ]……. The with events keyword is used only in class as modules. This keyword specifies that the varname is an object variable used to respond to events triggered by an active X object The varname identifier is the name of the variable you are declaring. Use subscript if you are declaring an array The new keyword enables creation of an object. Type argument specifies the data type of the variable e.g. byte if you don‘t specify any type then its variant. Examples DIM emploid AS integer DIM name AS string NB : 1) Static keyword can be used instead of Dim in a procedure if you want the variable to retain its value when the procedure ends. 2) Re Dim keyword can be used to set up arrays if you want to be able to change the size of the array during execution of the program LOGICAL TESTING Logical testing may be performed upon string or numeric expression or upon controls. The expression string or numeric may contain variables, literal values, functions and arithmetic‘s or other operations as long as the expression produces values. A test will result in true, false, or null value. Null occurs when one or more of the expressions being tested has a null value. COMPARISON OPERATORS VB supports the usual set of relational or comparison operators, plus two unique to the language. 1. = = equal to 2. < > not equal to 3. < less than 4. > greater than 5. Z = =less than or equal to 6. > = greater than or equal to 7. LIKE- fuzzy string comparisons 8. IS- compares control values ARITHMETIC OPERATORS The following is the list of arithmetic operators supported by VB in order of precedence 1. Exponentiation ^ 2. Negation 3. Multiplication and Division * / 4. Integer division / Prepared By MR.ONESMUS

8

@computer Studies Department

GANANA INSTITUTE OF MEDICAL SCIENCES AND TECHNOLOGY 5. Modules arithmetic MOD 6. Addition and Subtraction + 7. String concatenation & LOGICAL OPERATORS There are six logical operators, which are mainly used to combine two or more relational operations in a test .These include 1. NOT its used with a single expression to reverse it‘s value so that true becomes false and vise versa e.g.……NOT(X>1000)…. 2. AND it links two expressions and is true if both expressions are true e.g …X.>1000 AND Y>1000…… 3. OR it‘s true i...


Similar Free PDFs