Академический Документы
Профессиональный Документы
Культура Документы
Save
Delete
Search
Student
Roll Number 1 2 3 Name Raj Amit Rahul Address c-65, hari nagar a-45, Ramesh ngr. c-96, Janak puri Phone 25126633 25165263 25545298
1. 2. 3. 4.
Datatype
Byte Boolean Integer Long
Range
0-255 True or False -32768 to +32767 -2147483648 to +2147483647
5. Single
6. 7. 8. 9.
8 bytes 8 bytes 14 bytes 8 bytes 4 bytes 10 bytes length of string 16 bytes 22 bytes + string length
10. Object 11. String (variable length) 12. String (fixed length) 13. Variant (numeric) 14. Variant (text)
Jan 1, 100 to Dec 31, 9999 Any object reference + string length 0 to approx. 2 billion 1 to approx. 65,400
Or Or Or
. .
Or Or Or
false
Or Or Or Or Or
Literals
String literals: raj, a-45, hari nagar, 845.73, 8-15-2005 Boolean literals: True, false Date / Time literals: #8-12-2005#, #1:45 AM # Single / Double literals: 12.5#, -13.80 , +0.73, 85.2! byte / integer / long literals: +45, -33, 0, 48, 128, 85%
Comments
The word REM or symbol () tells the visual basic that everything on that line following REM or () is not the code, it is a comment and should be ignored by the compiler. REM can appear only in the beginning of the line but apostrophe () can appear after a statement also. Example: Dim x% declares x as integer variable REM example of a comment.
Operators
1. 2. 3. 4. Arithmetic Operators String or Concatenation Operators Comparison Operators Logical Operators Arithmetic operators +, -, *, /, \ , mod, ^ 2+5 daljeet + singh 91 + 6 5/2 11/3 5\2 11\ 3 5 mod 2 11 mod 3 2 ^ 3 = 23 3 ^ 3 = 33 Concatenation operators(&) . . . . . . . . . . . 7 daljeetsingh 916 2.5 3.66 2 3 1 2 8 27
. . . . . . .
Comparison operators = 2=3 <> 9< >7 > 8>5 >= 5>=5 < 91<15 <= 9<=8 Logical Operators AND, OR, NOT, XOR (5 > 7 AND 9 <= 81) false true
. Truth Table A OR B A XOR B False False True True True True True False
false
Control Name
Label
Property
Name Appearance Alignment Autosize Backcolor Backstyle Borderstyle Caption Enabled Font Forecolor Height Left Tooltiptext Top Visible Width
Value
0 flat 1 3D 0 left justify 1 right justify 2 center true / false 0 transparent 1 opaque 0 none 1 fixed single true / false
true / false
Control name
Textbox
Property
Name Alignment Backcolor Enabled Font Forecolor Height Left Maxlength Multiline Passwordchar Scrollbars
Value
0 left 1 right 2 center True / false
Width
true / false
Control Name
Command button
Property
Name Appearance Backcolor Caption Disabledpicture Downpicture Enabled Font Height Left Picture Style Tooltiptext Top Visible Width
Value
0 flat 1 3D
true / false
Control Name
Frame
Property
Name Backcolor Borderstyle Caption Enabled Font Forecolor Height Left Tooltiptext Top Visible Width
Value
0 none 1 fixed single true / false
true / false
Control Name
Checkbox
Property
Name Allignment Backcolor Caption Disabledpicture Downpicture Enabled Font Forecolor Height Left Picture Style Tooltiptext Top Value Visible Width
Value
0 left 1 - right
true / false
Control Name
Optionbutton
Property
Name Allignment Backcolor Caption Disabledpicture Downpicture Enabled Font Forecolor Height Left Picture Style Tooltiptext Top Value Visible Width
Value
0 left 1 - right
true / false
Control Name
Picturebox
Property
Name Align
Value
0 - None 1 Align top 2 Align bottom 3 Align left 4 Align right True / false 0 none 1 fixed single true / false
Autosize Backcolor Borderstyle Enabled Font Forecolor Height Left Picture Tooltiptext Top Visible Width
true / false
Control Name
Image
Property
Name Borderstyle Enabled Height Left Picture Stretch Tooltiptext Top Visible Width
Value
0 none 1 fixed single true / false
Control Name
Combo box
Property
Name Backcolor Enabled Font Forecolor Height Left List
Value
True / false
true / false
Control Name
Listbox
Property
Name Backcolor Enabled Font Forecolor Height Left List Multiselect Style Tooltiptext Top Visible Width Text
Value
True / false
Control Name
Hscrollbar / vscrollbar
Property
Name Enabled Height Left Max Min Top Value Visible Width
Value
True / false 32767 0 True / false
Control Name
DriveListbox
Property
Name Backcolor Enabled Font Forecolor Height
Value
True / false
True / false
Control Name
Directory list box
Property
Name Backcolor Enabled Font Forecolor Height Left Tooltiptext Top Visible Width Path
Value
True / false
True / false
Control Name
File list box
Property
Name Backcolor Enabled Font Forecolor Height Left Multiselect Pattern Tooltiptext Top Visible Width Path
Value
True / False
True / false
Control Name
Shape
Property
Name Backcolor Backstyle Bordercolor Fillcolor Height Left
Value
0 transparent 1 opaque
Shape
0 rectangle 1 square 2 oval 3 circle 4 rounded rectangle 5 rounded square True / false 0 solid 1 - transparent
Control Name
Timer
Property
Name Enabled Interval Left Top
Value
True /False
Control Name
Form
Property
Name Backcolor Caption Enabled Font Forecolor Height Left Mdichild Maxbutton Minbutton Picture Top Visible Width Windowstate
Value
True / false
True / false True / false True / false True / False 0 normal 1 minimized 2 - maximized
Events
1.
Event refers to the occurrence of an activity. Some useful events Click:- When the user clicks the primary mouse button on an object. Change:- When the user modifies text in combo box or text box. Gotfocus:- When an object receives focus. Lostfocus:-When an object loses focus. Keypress:- The user presses and releases a keyboard key while an object has focus. Keyup:- When the user releases a keyboard key when an object has focus. Dragdrop:- The use drags an object to another location. Dragover:- The user drags an object over another control. Form_load: - When the form loads for execution in memory. Setting the Properties
2. 3.
4. 5. 6. 7.
8. 9.
The controls that you draw on your form, have some properties associated with them. The properties can be set by two different ways. 1. At design time 2. At run time.
1. Properties can be set at design time using properties window.
2. Setting properties at run time. Setting the Alignment property Example: Label1.alignment = 0 Or Text1.alignment = 1 Setting Appearance property Example: Label1.appearance =1 Or Command1.appearance = 1 Or Option1.appearance = 0 Setting Autosize Property Example: Label1.autosize = true
Or Label1.autosize = false Or Picture1.autosize = true Setting the backcolor property Example: Label1.backcolor = RGB(100, 125, 190)
0-255 (red) Or Or text1.backcolor = VbRed Setting Backstyle Property Example: Label1.Backstyle = 0 Or Label1.backstyle = 1
0-255(green)
0-255( blue)
Setting the Enabled Property Example Text1.enabled = true Or Label1.enabled = false Setting caption property Example: Label1.caption = this is my name Or Command1.caption = click me Setting forecolor property Example: Label1.forecolor = RGB( 100, 255, 70) Or Label1.forecolor = VbBlue Setting Height Property
Example: Label1.height = 100 Or Command1.height = 200 Setting the tooltiptext property Example: Text1.tooltiptext = This is a text box Setting the text property Example: Text1.text = Amit sharma Setting the visible property Example: Label1.visible = true Or Text1.visible = false Setting the width property Example: Text1.width = 250 Or Command1.width = 100 Setting the style property Example: Command1.style = 0 Or Option1.style = 1 Setting the picture property Example: Check1.picture = loadpicture ( c:\abc.gif) Or Command1.picture = loadpicture ( c:\abc.gif) Or Option1.picture = loadpicture ( c:\abc.gif) Or Picture1.picture = loadpicture ( c:\abc.gif) Setting the disabledpicture and downpicture property Example: Command1.disabledpicture = loadpicture(c:\abc.gif) Or
Command1.downpicture = loadpicture(c:\abc.gif) Setting the value property of checkbox Example: Check1.value = 0 Or Check1.value = 1 Setting the value property of option button Example: Option1.value = true Or Option1.value = false Setting the stretch property of image control Example Image1.stretch = true Or Image1.stretch = false Setting the max/min property of Hscrollbar / Vscrollbar Example Hscroll1.max = 100 Hscroll1.min = 1 Vscroll1.max = 100 Vscroll1.min = 1 Setting the fillstyle and fillcolor property Example: Shape1.fillstyle = 0 Shape1.fillcolor = RGB(100, 150, 200) Setting the Shape Property of Shape control Example: Shape1.shape = 1 Or Shape1.shape = 3 Setting the windowstate property of form Example: Form1.windowstate = 0 Or Form1.windowstate = 1
Some Examples: Example1:- Loading a picture in the picture box at run time. Picture Box
Load
Command Button (cmdload) Private sub cmdload_click() Picture1.picture = loadpicture (c:\abc.gif) End sub Example2: Loading a picture in the image control at run time. Image control(image1)
Load
Command Button( cmdload ) Private sub cmdload_click() Image1.picture = loadpicture (c:\abc.gif) End sub Example3: List Box
ListBox (list1)
Private sub cmdadd_click() List1.additem text1.text End sub Private sub cmddel_click() List1.removeitem list1.listindex End sub Private sub cmdclear_click() List1.clear End sub Private sub cmdcount_click() Msgbox List1.listcount End sub Private sub cmdindex_click() Msgbox list1.listindex End sub Private sub cmddisp_click() Msgbox list1.text End sub Adding an item in the list box List1.additem Ravi List1.additem text1.text Removing an item from the list box List1.removeitem 0 List1.removeitem list1.listindex Example 4: Hscrollbar / Vscrollbar application Hscroll1
Sum
vscroll1
Private sub hscroll1_change() Text1.text = hscroll1.value End sub Private sub Vscroll1_change() Text2.text = vscroll1.value End sub Private sub cmdsum_click( ) Dim a as integer, b as integer a = val (text1.text) b = val (text2.text) text3.text = a + b End sub
Rectangle
Square
Oval
Circle
Rounded Rectangle
Rounded square
Private sub cmdrect_click( ) Shape1.shape = 0 End sub Private sub cmdsquare_click( ) Shape1.shape = 1 End sub Private sub cmdoval_click( ) Shape1.shape = 2 End sub Private sub cmdcircle_click( ) Shape1.shape = 3 End sub Private sub cmdroundrect_click( ) Shape1.shape = 4 End sub Private sub cmdroundsquare_click( ) Shape1.shape = 5 End sub
file1
Private sub drive1_change( ) Dir1.path = drive1.drive End sub Private sub dir1_change( ) File1.path = dir1.path End sub Private sub text1_change( ) File1.pattern = text1.text End sub Example 7: Timer application. Label1
Set the following properties: Timer1.interval = 1000 Label1 - > font = MS sans serif, bold, 18
Dim I as integer Private sub cmdreset_click( ) I=0 End sub Private sub timer1_timer ( ) Label1.caption = i I=I+1 End sub Example 7: Option Button Application.
Percentage
90
80
70
60
Click
lblgrade
cmdgrade
Private sub cmdgrade_click( ) If optone.value = true then Lblgrade.caption = The grade is A Else if opttwo.value = true then Lblgrade.caption = The grade is B Else if optthree.value = true then Lblgrade.caption = The grade is C Else if optfour.value = true then Lblgrade.caption = The grade is D End if End sub Private sub form_load( ) Optone.value = false Opttwo.value = false
Optthree.value = false Optfour.value = false End sub Manipulating forms Setting the startup form To change the startup form 1. Goto project menu - > project peoperties 2. Select General Tab 3. In startup object listbox select the form 4. Click Ok Loading and unloading the forms Load form1 Unload form1 Unload me Showing and Hiding forms Form1.show Form2.show Form1.hide Me.hide Assigning the Access key Command buttons can be used by clicking on them or by pressing the alt + < access key >. To assign a keyboard access key to command button place ampersand( &) in front of the letter that is to be used as access key, while setting the caption property of the command button.
Control Structures in VB
Control Flow:
1. 2. 3.
Example:
If (a>10) then Print Raj End if
Example:
Enter the marks
Text1
Result cmdresult Private sub cmdresult_click( ) Dim m as integer m = val (text1.text) if (m >= 40) then print you are passed else print you are failed end if End sub
Example:
Text1 Enter a number
Print day cmdprintday Private sub cmdprintday_click( ) Dim x as integer X = val(text1.text) If(x = 1) then Msgbox Sunday else if (x = 2) then Msgbox Monday else if (x = 3) then Msgbox Tuesday else if (x = 4) then Msgbox Wednesday else if( x = 5) then Msgbox Thursday else if (x = 6) then
Msgbox Friday else if (x = 7) then Msgbox Saturday Else Msgbox Invalid choice End if End sub
Example:
If( x >= 91 OR p <> 83) then Print Rishabh Else Print Amrita End if
Example:
Dim num1 as integer, num2 as integer, big as integer
1. 2. 3. 4.
Example 1:
Text1 Enter a number
Print day cmdprintday Private sub cmdprintday_click( ) dim m as integer m = val( text1.text ) select case m case 1: Msgbox Sunday case 2 Msgbox Monday case 3 Msgbox Tuesday case 4: Msgbox Wednesday case 5: Msgbox Thursday case 6: MsgboxFriday case 7: MsgboxSaturday case else Msgbox Invalid choice End select End sub
Example 2:
Result cmdresult Private sub cmdresult_click( ) Dim m as integer M = val( text1.text ) Select case m Case is < 50 Msgbox Fail Case is < 60: Msgbox Grade B Case is < 75: Msgbox Grade A Case else: Msgbox Grade A+ End select End sub
Example 3:
txtmonth Enter the month no.
No. of days cmdnod Private sub cmdnod_click( ) Dim n as integer n = val ( txtmonth.text ) select case n
case 1,3,5,7,8,10,12 Msgbox 31 days in the month case 2 Msgbox 28 days in the month Case 4,6,9,11 Msgbox 30 days in the month End select End sub
Looping Structures:
1. 2. ForNext Do loop (a) Do whileloop (b) Doloop while (c ) Do untilloop (d) Doloop until whilewend
3.
1.
Fornext Example:
For i = 1 to 10 step 1 Print Raj Next i
Example:
For i = 7 to 22 step 2 Print abc Next i
Example:
For x = 17 to 1 step -3 Print raj Next x
2.(a)
Do whileloop Example:
x=1 Do while (x < = 10) Print xyz x=x+1 loop
Example:
x = 10 Do while (x > = 1)
Example:
x=1 Do while (x < = 20) Print raj x=x+2 loop
Example:
x = 10 do print raj x=x-1 loop while (x >= 1)
Until
x >= 10 x > 10 x <= 1 x<1 x=1 x <> 1 x=1 do until x > 10 print Raj x=x+1 loop
Example:
Example:
x = 10 do until x < 1 print Raj x=x-1
loop
Example:
x = 10 do print Raj x=x-1 loop until x < 1
Example:
x = 10 while x >= 1 print raj x=x-1 wend
Nested loops
next j next i Example: i=1 do while i<=3 j=1 do while j<=5 print "raj" j=j+1 loop i=i+1 loop
Arrays
Array is a collection of variables of same type that are referenced by a common name. Example: Private sub Command1_Click() dim a(4) as integer dim i as integer for i=0 to 4 step 1 a(i) = inputbox "enter a number " next i for i=0 to 4 step 1 print a(i) next i End Sub
Redim statement
Example: Private sub Cmdprint_Click( ) dim a( ) as integer dim i as integer redim a(4) for i=0 to 4 step 1 a(i) = inputbox "Enter a number" next i for i=0 to 4 step 1 print a(i) next i redim a(9) print "array after re-dimensioning " for i=0 to 9 step 1 print a(i) next i End sub Redim preserve a(9)
Preserve statement
Print message
cmdprintmessage Private sub cmdprintmessage_click( ) print "Raj" print "Daljeet" disp or call disp print ravi print "kailash" call disp print "vishal" End sub Private sub disp( ) print "Harish" print "Manish" print "Vicky" End sub Example: Procedures with arguments Enter a number Enter second number Result is sum text 3 text 1 text 2
cmdsum Private sub cmdsum_click( ) dim x as integer, y as integer x = val (text1.text) y = val(text2.text) call findsum (x, y) or findsum x, y findsum x . Error findsum x, 5, y . Error End sub Private sub findsum (n as integer, m as integer) text3.text = n + m End sub 2. Function Procedures: A function is a procedure that performs a specific task and returns a value. Types of Functions 1. User Defined Functions 2. Built in Functions or Library Functions
Example: Enter a number Enter a second number Result is Sum Text1 Text2 Text3
cmdsum Private sub cmdsum_click( ) dim x as integer, y as integer, z as integer x = val(text1.text) y = val(text 2.text) z = findsum(x, y) text3.text = z End sub Private function findsum (n as integer, m as integer) as integer findsum = n + m End function
1. Call by Value (pass by value) .Byval 2. Call by Reference (pass by reference) ..Byref Example:
cmdprint
Print Private sub cmdprint_click( ) dim x as integer x=5 print x call change(x) or change x print x End sub Private sub change (Byref n as integer) n=100 print n End sub
. . .
5 100 100
Private sub cmdsum_click( ) dim x as integer, y as integer x = val(text 1.text) y = val(text 2.text) findsum x, y findsum x findsum findsum 2, y, 5 . error End sub Private sub findsum (optional n as integer = 70, optional m as integer = 80) text3.text = n + m End sub
Enter a no. check cmdcheck Private sub cmdcheck_click( ) dim x as integer x = val(text1.text) call samplepro(x) End sub Private sub samplepro (n as integer ) if (n > 50) then msgbox "you have got a car" else Exit sub
text1
1. Lcase and Ucase Functions Print Ucase(hello) Print Lcase (HELLO) Text1.text = Ucase(text1.text) Text1.text = Lcase(text1.text) 2. Len Function dim x as integer x = len(raj) print x x = len(text1.text) print x print len(Rajeev)
HELLO hello
3 6
3. Trim, Ltrim, and Rtrim functions Text1.text = Ltrim( daljeet) Text1.text = Rtrim(sandeep ) Text1.text = trim( amit arora Text1.text = trim(text1.text) Text1.text = rtrim(text1.text) 4. Left and Right functions dim n as string, m as string m = daljeet n = right(m, 4) print n m = raj arora print left (m,3) 5. Mid function dim x as string, y as string x = raj kumar arora y = mid (x, 5, 5) print y print (x,11, 5) 6. Instr function
jeet raj
kumar arora
dim searchstring as string, searchchar as string, mypos searchstring = XXpXXpXXPXXP searchchar = P mypos = instr (4, searchstring, searchchar, 1) 6
mypos = instr(1, searchstring, searchchar, 0) mypos = instr(searchstring, searchchar) mypos = instr(searchstring, w) 7. Space Function dim p as string p = raj & space(10) & puri print p 8. String Function dim x as string x = string(10, a) print x x = string (5, ABC) print x 9. Str Function dim x as string, y as string x = str(1205) y = str(1305) print 12 + 15 print str(12) + str(15) print 12 + 15
raj
puri
aaaaaaaaaa AAAAA
10.Asc Function dim x as integer x = asc(A) print x 65 print asc(Abc) 65 x = asc(a) print x 97 ASCII Values American Standard Codes For Information Interchange. A to Z 65 to 90 a to z 97 to 122 0 to 9 48 to 57 11.Chr Function dim x as string x = chr(65) print x A print chr(66) B x = chr(98) print x b 12.Strreverse Function Dim x as string x = Neeraj
jareeN jareen
Numeric Functions
1. Int & Fix Functions print int(14.1) . print int(14.6) . print int(-14.1) . print int(-14.6) . print cint(14.1) print cint(14.6) print cint(-14.1) print cint(-14.6) print fix(14.1) print fix(14.6) print fix(-14.1) print fix(-14.6) 2. Sgn Function if number is greater than 0 equal to 0 less than 0 print sgn(-5) print sgn(0) print sgn(10) x = sgn (-5) print x . . . . . . . .
14 14 15 15 14 15 14 15 14 14 14 14
3. Val( ) Function print 2+5 print 2 + 5 print val(2) + val(5) print val(18th Road)
4. Rnd Function Text1.text = rnd(1) Or print rnd(3) To produce a random number within the given range. int ((upperbound lowerbound + 1) * rnd + lowerbound) Example: (To produce a random number between 1 and 6) dim x as integer
Or
2. Date and Date$ Functions Print date . Print date$ . Date Variant Uses / Do not places 0 Do not appends 19 or 20 before year 3. Time and Time$ Functions Print time . Print time$ .
1/17/04 01-17-2004 Date$ String Uses - Places 0 Appends 19 or 20 before year 9:11:50 PM 21:11:50
Time Time$ Variant String 12 hour 24 hour 4. Datepart Function Syntax: Datepart (interval, validdate) Interval yyyy year q quarter m month y day of the year d day w weekday ww week h hour n minute s second Example: dim x as date x = #1/17/2006# Print datepart (m, x) Print datepart(yyyy, now) 5. Day, Month and Year Functions
1 2006
Print day(date) Print month (date) Print year(date) dim x as date x = #1/19/2006# print year(x) print day(x) 6. dim x as date x = #12:10:41 AM# Print hour(x) Print minute(x) Print second(x) Print minute(now) 7. Print timer Text1.text = timer( )
12 10 41 Timer( ) Function
Or
8. Dateadd and Datediff Functions Syntax: Dateadd (interval, number, olddate) Example: x = dateadd(y, 25, now) Or x = dateadd(yyyy, 3, now) Datediff Function Syntax: Datediff(interval, date1, date2) Example: x = #1/17/2006# y = #2/25/2004# n = datediff (yyyy, x, y)
1. Isdate( ) Function dim x as integer, y as string x = 10 y = 12/17/2005 print isdate(x) print isdate(y) 2. Isnumeric( ) Function print isnumeric(x) print isnumeric(y)
. . . .
3. Isempty( ) Function 4. Isnull( ) Function 5. Vartype( ) Function Example: dim x as integer, y as string Print vartype (x) Print vartype (y) 6. Inputbox( ) Function Syntax: Inputbox (prompt [,title] [,default] [,xpos] [,ypos]) Example: p = inputbox(Enter the number, My Project, 10, 100, 200) 7. Msgbox( ) Function Syntax: Msgbox (prompt [,buttons] [,title]) Example: Msgbox (A Trial Message) x = Msgbox (Do you wish to continue:, 4 + 32, My Project)
. .
2 8