Sie sind auf Seite 1von 25

Chapter-1 Introduction to Visual Basic GUIA GUI is a graphical (rather than purely textual) user interface to a computer.

The term came into existence because the first interactive user interfaces to computers were not graphical; they were text-and-keyboard oriented and usually consisted of commands you had to remember. The command interface of the DOS operating system (which you can still get to from your Windows operating system) is an example of the typical usercomputer interface (character/command user interface-CUI) before GUIs arrived. An intermediate step in user interfaces between the command line interface and the GUI was the non-graphical menu-based interface, which let you interact by using a mouse rather than by having to type in keyboard commands. Today's major operating systems provide a graphical user interface. Applications typically use the elements of the GUI that come with the operating system and add their own graphical user interface elements and ideas.

Programming languagesThere are various types of programming languages, each was developed to solve a particular kind of problems. 1. Procedural languagesA computer programming language that follows, in order, a set of commands. Examples of computer procedural languages are BASIC, C, FORTRAN, and Pascal. 2. Object Oriented programming languageA type of programming in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure. In this way, the data structure becomes an object that includes both data and functions. In addition, programmers can create relationships between one object and another. For example, objects can inherit characteristics from other objects.

3. Event Driven programming languageIn computer programming, event-driven programming also known as event-based programming is a programming method in which the flow of the program is determined by user actions such as mouse clicks, key presses.

VB EnvironmentVB is event-driven programming language, which has some of the features of object oriented programming language. In VB you will work with objects, which has properties and methods. ObjectsObject is an element from a VB program, such as a control, form or code module that holds programming statement. PropertiesA Property helps to differentiate a control from other control because the property shows appearance and behavior of a control. Properties have values such as colors, names, size and location on the form. MethodsActions associated with the objects are called methods. Some common methods are move, print, show etc. EventEach action by the user causes an event, Event is an activity that occurs during program execution, such as mouse click, key-press, activate etc.

VB EnvironmentAn integrated development environment (IDE) is a programming environment that has been packaged as an application program, typically consisting of a code editor, a compiler, a debugger, and a graphical user interface (GUI) builder. Main Window- Title bar, Menu bar & tool bar Tool Box Form Window Project Explorer Properties window Form Layout window

Visual Basic Projects-

The Project file with extension .vbp Each form in project saved with extension .frm Optionally you can have .bas files that contains basic statement accessed from any form of the project. (These files are also called standard code module) If you include additional controls which are not part of standard control set then the .ocx file names will be included in your project. After you save a project, VB automatically adds one or more files to your project with extension .vbw(VB project workplace). This file holds information about each of your projects form.

Errors in VB1. Compile time errors- (Occurs when you press enter key or when you execute code) Project code->m/c code=>Compile time errors if you break syntax rule Ex. if a<b if without then gives compile time error print a else print b end if

If you spelled wrong ennd

ennd instead End

Run Time ErrorsIf your project halts during execution then its runtime error Common runtime errors Divide by zero Performing an arithmetic operation on nonnumeric data Find a square root of negative number Illegal use of object
Logical Error-

Naming rules and conventions for objects RulesBegin with a letter 40 char long Letter, digit and underscores

Conventions-Follow this to make project more understandable


Always begin a name with a lowercase three-letter prefix and capitalize the first character after the prefix. EX- cmdShow, lblMessage, optColor For name with multiple word ,capitalize first letter of each word frmDataEntry, chkPrintForm

Visual Basic StatementsRemark statement-To add comments,Use apostrophe to start comment. Assignment Statement-Assign value to variable or value to property. End Statement-Stops execution of program

Project Execution and debuggingExecutionF5 Start Start with full compilation Debugging-F8

Variables & Constant


VariablesMemory location that holds the data that can be changed during program execution is called Variable. Constant Memory location that holds the data that can not changed during program execution is called Constant. Identifier When you declare a variable VB reserves memory space & assigns a name called identifier.

Naming Rules & conventions for Variable & ConstantRules 1 to 255 chars long Combination of digits, alphabets & underscores They may not be reserved words Conventions Must be meaningful Choose a name that indicates purpose of variable/constant Use prefix before name Capitalize First name of the word Ex- intEmpNum dtm B_Date dblPer

Data types in VB Boolean Byte Currency Date Double Integer Long Single String Variant

2 bytes 1 byte 8 bytes 8 bytes -------2 bytes 4 bytes 4 bytes fixed or variables 22 bytes

True/False 0 to 255 Decimal fractions (dollars,Rs.) mm/dd/yyyy floating point number whole numbers (-32768 to 32767) Long whole numbers floating point with 6 digits accuracy 1 to 65000 chrs Converts data according to value.

ExamplesDim s as string*3 S=VBProgramming Print s Output-VBP Dim s as variant S=10 Print s=10 S=hello Print s

Variable declarationDim identifier as [type]


Dim-Dimensions (size) Type is optional if omitted default is variant

OPERATORS : Arithmetic Operators :


Operator
+ -

Example
A+B A-100

Description
Add two values Subtract second val from first

*
/ Mod

Total*Fact
Tax/a Num1 Mod Num2

Multiply two values


Divide one no by other Divide 2 nos and return only remainder

^
& or +

No1 ^ Pow
Name1 & Name2

Raises a value to a power


Concatenates 2 strings

Operator + * / Mod

Description Add two values Subtract one value from other Total*fact Multiply two values. Tax/a Divide one number by another number Num1 mod num2 Divides two nos. and return only remainder. Num^exp Raises a value to a power

Example A+B A-400

Comparison Operators :<,>,<=,>=,<> Used to compare expressions Example : This example shows various uses of comparison operators, which you use to compare expressions. Dim MyResult MyResult = (45<35) 'Returns False. MyResult = (45=45) 'Returns True. MyResult = (4 <> 3) 'Returns True. Like Operator : Syntax- string like pattern Dim MyCheck MyCheck = "aBBa" Like "a*a" 'Returns True. MyCheck = "F" Like "[A-Z]" 'Returns True. MyCheack = "CAT123khg" Like "B?T*" 'Returns False.

Case statement

SyntaxSelect Case VARIABLE Case expression1 : ------Case expression2:-------. . . [Case else: ] End select

*Expression can be any numeric or string value

Select statement can handle a single value, ranges and sets


Example-Select case with single valueWrite a program to add, sub, div and multiply 2 numbers using case statement.
Dim a As Integer, b As Integer, ope As String a = Val(Text1) b = Val(Text2) ope = Text3 Select Case ope Case "+": Print a + b Case "-": Print a - b Case "*": Print a * b Case "/": Print a / b Case Else: Print "Wrong selection" End Select

Example- Select case with Sets and range of values WAP to print given letter is an alphabet, a number or a symbol
Dim a As String a = Text1 Select Case a Case 0 To 9: Print "Numbers" Case "A" To "Z", "a" To "z": Print "Alpahabets" Case Else: Print "Symbol" End Select

Example- Select case with Relational Operator WAP to print grade for given percentage Dim per As Integer per = Val(Text1) Select Case per Case Is < 40: Print "FAIL" Case Is < 60: Print "PASS" Case Is < 70: Print "First" Case Is > 70: Print "DIST" End Select

CHAPTER -1 1.Write diff. between method, property and event. 2. What is the purpose of these VB files typevbp, frm, bas & ocx. 3. What is visual programming? 4. What is the diff. between conventional programming and visual programming.

Assinment-2 1. Explain following tools with its five imp. properties.Form, command button, Label, Text box, option button and check box. 2. Write data types in VB. 3. What are the conditional statements in VB? Explain with example. 4. What are variables and constants in VB? Write Naming rules and conventions for Variable.

5. Give difference between declaring a variable in general section, module level declaration and global declaration. 6. Write short note on operators in VB 7. What is variant data type? State its advantages and disadvantages.
Programming Assignment Create a 7 digit Calc in VB which perform following basic operation- addition, subtraction, Multiplication and division and memory storing, canceling and recalling.

Das könnte Ihnen auch gefallen