项目二 visual basic 程序设计基础

Click here to load reader

Upload: rosalia-woriega

Post on 31-Dec-2015

307 views

Category:

Documents


0 download

DESCRIPTION

项目二 Visual Basic 程序设计基础. 【 教学目的 】1 、掌握常用的基本数据类型 2 、掌握常量和变量的应用 3 、理解 VB 常用内部函数 4 、掌握运算符和表达式的应用 5 、掌握数组的定义和使用 6 、掌握标准函数的使用 7 、掌握过程和函数的应用 【 教学重点 】 变量和常量的定义;函数、运算符、表达式的正确使用;数组的定义和使用。 【 教学难点 】 变量的定义、数组、函数的使用。. 活动一. 2.1 数据类型. 2.2 常量与变量. 2.3 运算符和表达式. 2.4 标准函数. 2.5 基本语句. - PowerPoint PPT Presentation

TRANSCRIPT

  • Visual Basic123VB4567

  • 2.1 2.2 2.3 2.4 2.5 2.6 2.7

  • 2.1 TrueFalse-123%123%4-1234&987654&Ee7-1.234567E6

    Dd1516-1.112223334445556D-338154546.5465@##January 15,2002##1985-10-1 9:45:00 PM# #3-6-93 13:20#abcdef VariantVB

    Byte10~255Boolean2TrueFalseInteger%2-32768~32767Long&4-2147483648~2147483647Single!4-3.402823E38~-1.401298E-451.401298E-45~3.402823E38Double#8-1.79769313486232D308~-4.94065645841247D-3244.94065645841247D-324~1.79769313486232D308Currency@8-922337203685477.5808~922337203685477.5907Date(time)801,01,100~12,31,9999String()$0~65535String()Variant

  • 1VB23.234538.6787E+8A) 86787000 B) 867870000 C) 8678700 D) 867870000042.65358979335278E-006A) 0.0000265358979335278B) 0.00000265358979335278C) 000265358979335278D) 0.000000265358979335278

  • VBTypeType As As End Type

  • Type Client_Record cltName as String*16 cltPassword as String*8 cltDeposit as CurrencyEnd Type1PublicTypePrivate2strName As String*Client_Record3 cltName cltPassword cltDeposit

  • 2.2.1 1

    2.2Visual Basic6.012

  • (1) 65535(2)&H&O 1200 '1200 &H333 '333 &O555 '555

  • EE1012.345E-678E30.000012 34578 00071516(3)FalseTrue(4)##2007-9-9##January 15,2002# #3-6-93 13:20#

  • VB 2345VB2345 %@$

  • 2

    255 VB Const Const Const [As ] = AS Const c1 As String= ( Const day = #09/30/2003 # Const pi! = 3.14159

  • 2.2.2

    1VB372VB255temp#3VBcaption caption$ 45strNameintAge

  • VB

    Dim As [, As ] Public As [, As ] Private As [, As ] Static As [, As ]

    DimPublic Private /Static

  • VBVB3VB3(Form)(Module)(Class) Form(Declaration)(General Procedure)(Even Procedure) (Module)VB

  • 1DimStatic Dim As [, As ] Dim intX As integer (intX) Dim total As Single (total) Dim strName As String*10 (strName10) Dim stra1 As String (stra1) Dim Dim str1 As String, str2 As String, ff As Boolean DimAs Dim str1,str2 As String, ff,var1 As Long

  • Static As [, As ] Dim Static StaticDim0Static num As IntegerPrivate Sub Command1_Click() Static x As String Dim y As String x = x + "a" y = y + "b" Print "x"; x Print "y"; yEnd SubPrivate Sub Command1_Click() Static x As Integer Dim y As Integer x = x + 1 y = y + 1 Print "x"; x Print "y"; yEnd Sub

  • Private sub command1_click() Dim num1 As Integer Static ss as String End Sub Private sub command2_click() Dim num1 As Integer Static ss as Integer End Sub1 3ClickStaticxx=x+1

  • 2()()Form1Form2PrivateDimPrivate As [, As ] Private x as Integer Form1Temp. Form1Temp Dim temp As StringModule1temp

  • 3PublicGlobalPublic As [, As ] Public Public Public intTemp As Integer temp1Form1Form2temp - Public temp As Integer

  • 5DimPublic4Option Explicit VBVariant)

    DimStaticDimPrivatePublicGlobal

  • Private Sub Command3_Click() var2 = 400 Print var1, var2End Sub

    Private Sub Command4_Click() Static var4 As Integer var4 = var4 + 1 Print var4; End Sub

    Private Sub Command5_Click() Dim var5 As Integer var5 = var5 + 1 Print var5; End SubOption ExplicitDim var1 As IntegerPrivate var2 As Integer

    Private Sub Command1_Click() var1 = 100 var2 = 300 Print var1, var2End Sub

    Private Sub Command2_Click() Dim var1 As Integer var1 = 50 var2 = 200 Print var1, var2End SubPrint

  • 1VBA) 4*delta B) AlphaC) 4abcD) ab2VBA) FilenameB) A(A+B)C) 368DD) Print

  • 11Form1Form2Command1Caption 1AllM1Form1M2Form2L1Form1Command1_Click 2Form1M106ALL123Form2M2VB 3Form1Command1L13.14159Text1AllText2L1Label1M1Form1Command2Form2 4Form2Command1Text1M2Text2AllForm2M1

  • 2.3.1 12342.3

  • 1.VB884-6

    ^13^29-2-2-2*32*510/34/50.8\44\50Mod()55 Mod 41+62+3564.5-2.42.1

  • 2.&+4-7 123 + 456 123456 123 & 456 123456 : + , & : abcdef + 12345 abcdef & 12345 abcdef12345 123 + 456 579 123 & 456 ' " 123456 "

    &"Micro" & "soft""Microsoft"+"123"+"456""123456"

  • 3.TrueFalseVB-10TrueFalse4-8VB

    ="abc"="ABC"False>"abc">" ABC "True>=34>=42False>>5a

  • 1:110 \ 2.4; 10 \ 3.6;14 \ 4 28*3+2 Mod 3 / 2 33*3>=84 "EFG""efg" & "abc"5Not 5=4 And 53*2 Or 78 And 8=2*4 710>12+3 And True And 2>38Not 7>2*4 Or Not 52+3 And 2 Mod 4 =02 Ayear4100400 Bax2+bx+c=0a0b2-4ac0 Csexage1820(size)1.6516181.60Dmarrigerat(age)26workingage5

  • 2\/Mod*4 A\B/CModD* 3AB00 AA*B=0ANDABBA=0ORB=0ANDAB CA=0ANDB0ORA0ANDB=0 DA*B=0ANDA=0ORB=0 4ABC3 AA+B>=COrA+C>=BOrB+C>=ABNot(A+B
  • 1VB(/\Mod)555(/)(\)(Mod)5 (/\Mod)3-3Text1Text2Label1Label34Caption+

  • 2.4 2.4.3 2.4.4 2.4.1 2.4.2 2.4.5 2.4.6

  • 2.4.1

    Sin(x)xSin(1).841470984807897Cos(x)xCos(1).54030230586814Atn(x)Atn(1).785398163397448Tan(x)xTan(1)1.5574077246549Abs(x)xAbs(-2.4)2.4Sgn(x)xx-1x00x1Sgn(-100)-1Sqr(x)xSqr(16)4Exp(x)exexExp(2)7.38905609893065Log(x)xexLog(2)0.69314718055994

  • IntFixIntFixInt(x)Fix(x)IntXFixX Int(-3.6) Fix(-3.6) Int(3.6) Fix(3.6) -4 -3 3 3

  • Hex$(x) Oct$(x) x x

  • 1INT8*SQR36*10^-2*10+0.5/10

  • 2.4.2

    Ltrim()LTrim$("MyName")"MyName"Rtrim()RTrim$("MyName")"MyName"Trim()Trim$("MyName")"MyName"Left(,N)NLeft$("MyName",2)"My"Right(,N)NRight$("MyName",4)"Name"Mid(,N1[,N2])N1N2Mid $("MyName",2,3)"yNa"Len()Len("MyName")6

  • InStr()N1MC1N1C2N10M0120120

  • Len()Len() Len()Left()Right()Left(,n)Right(,n)nnLeft(,n) nRight(,n) n

  • Mid()Mid(,n1,n2)n1n2n1n2 Ltrim()Rtrim()Trim()Ltrim()Rtrim()Trim()Ltrim()Rtrim()Trim()

  • String()String(n,c)ncASCIIn Space()Space(n)nn

  • 1Mid("ABCDEFG",2,3) 2InStr(2, "ABCDEFG", "EF") 3Right(abcdefghi,4)4String(4,abc)5Len(123.4)6Ucase(Left(basic,3))

  • 2.4.3 IsArray( )IsArray()IsDate( ) IsDate()dim aa as string,bb as boolean aa=10-12-2007 bb=IsDate(aa) print bb

  • IsEmpty() IsEmpty()dim aa as string,bb as integer,cc as boolean cc=IsEmpty(aa) cc=IsEmpty(bb) aa=kkkk cc=IsEmpty(aa)IsNumeric() IsNumeric() dim a as booleana=IsNumeric(text1.text) print a

  • Str Str(X)X Long X 1Str(123) Str(-23.456)2.4.4

  • Val Val(string)2Val(-123.4)Val( 123.4)Val(a123.4)Val(12-3.4)

  • 3ASCAsc ASCAsc(string)string string 4ASCChr ASCASC Chr(charcode)charcode Longcharcode 0 2550 31 ASCII Chr(10) Chr(13)Asc(a)Asc(abc)Chr(65)Chr(97)

  • 2.4.5

    Now(yy-mm-dd hh:mm:ss)Now04-2-28 16:19:10Day(s)(1~31)Day("2002-3-5") Day(now)51WeekDay(s)(1~7)12WeekDay("2002-3-5")WeekDay(now)3()5()Month(s)(1~12)Month("2002-3-5")Month(now)3Year(s)(yyyy)Year("2002-3-5")Year(now)20022007Hour(s)(0~23)Hour(Now) 16Minute(s)(0~59)Minute(Now)31Second(s)(0~59)Second(Now)42

  • 2.4.6Rnd(x)10[]Int(( -+1*Rnd())+[50,100]Int((100-50+1)*Rnd())+50 Private Sub Command1_Click()Label1.Caption = Chr(Int((Asc("Z") - Asc("A") + 1) * Rnd() + Asc("A")))End SubPrivate Sub Command2_Click()Label2.Caption = Chr(Asc(Label1.Caption) + 32)End Sub

  • 1Int(-3.14159)2Fix(-3.1415926)3Sgn(25)4Sqr(Sqr(16))5Abs(98-100)6Str(-456.67)7Val(16Year)8Year(05-3-20)9WeekDay(Now)10Asc(ab)Rnd A0B1C0.1234D0.00005 INT100*RND1 A[099]B[1100]C[0100]D[199]

  • 1 x=2732.87 y=-658.236 z=3.14159*30/180 Int(x),Fix(x),Int(y),Fix(y),Hex(Int(x)),Oct(Fix(x)),Abs(y),Sin(z),Cos(z)

    -1*Sgn-100+Int(Rnd*100) A0B1C-1D [1037]VisualBasic AInt(Rnd(1)*27)+10BInt(Rnd(1)*28)+10 CInt(Rnd(1)*27)+11DInt(Rnd(1)*28)+11 Len(Str(Val(123.4))) A11B5C6D8

  • 2.5 1VB CAPTION caption Caption2 VB(:) x=10 : y=5 : z=6 325510234 _ Private Sub Form_MouseDown(Button As Integer, Shift As _Integer, X As Single, Y As Single)

  • 1

    1[ Let ]= Let2[ Let ][.]=2==x=10 : Text1.Text=abcd : Text1.Text=Text2.Text

    :Caption Texttext1.text=10

  • 2VBREM REM 1Rem2 3Visual BasicADim a(10)As Integer RemVB BVB Private Sub Command1_Click() Ca=1:b=2:RemVB:c=3 D If Shift = 6 And Button = 2 Then Print "BBBB" Rem VB End If

  • *2.6 VBVB

  • *VB1IF 2Select Case If x0 Then y=5*x+2 Else y=0 End If2.6.1

  • *If1If Then 2If Then End If FalseTrueTrue Then End If IfIf1If Then ()

  • *1If Then Else 2If Then 1 Else 2 End IfIfIfTrue Then Else 2If Then Else ()

  • *Private Sub Command_Click() Dim x as Integer x = val(Text1.Text) If x Mod 2 =0 then MsgBox Str(x) & Else MsgBox Str(x) & Enf if End Sub

  • *: If 1 Then 1 ElseIf 2 Then 2 ElseIf n Then n [Else n+1 ] End If1True 12True2n3IfThenElseIf ()

  • *Text1001010 Private Sub Command1_Click()Dim x As Integer x = Val(Text1.Text) If x < 0 Then MsgBox 0" ElseIf x < =10 Then MsgBox 010" Else MsgBox "10" End IfEnd Sub:0~60~6

  • *4IfIf Then ElseIf If Then

    End IfIf Then Else

    End IfIf Then End IfIf Then End IfIf If End If

  • 5IIf IIf IIf IIfTrueFalseIf Then Else x,yTmax Tmax=IIfx > yxyY = y=Iif(x
  • *Select CaseSelect Case Case [Case []] [Case []] [Case Else []]End SelectSelect Case y Case 1,3,5,7,9 print 10 Case 2,4,6,8 print 10 Case 10 to 20 print 1020 Case Else print End Select

  • 1,234,

  • strC If :If Ucase(strC) >="A" And Ucase (strC) ="0" And strC
  • Text1111010 Select Case Private Sub Command1_Click() Dim x As Integer x = Val(Text1.Text) Select Case x Case Is < 1 Text2.Text = "1" Case 1 To 10 Text2.Text = "1....10" Case Else Text2.Text = "10" End SelectEnd Sub

  • 1.x5"Result"Select Case( )A) Select Case x Case 10 To 1 Print "Result" End Select B) Select Case x Case Is 5, Is 5 Print "Result" End Select C) Select Case x Case Is 5, 1, 3 To 10 Print "Result" End Select D) Select Case x Case 1, 3, Is 5 Print "Result" End Select

    2.a=6 x=IIf(a5-10) x( )A) 5 B) 6 C) 0 D) -1

  • 3.r = 1 x0 2 x=0 ( )AIf x0 Then r = 1 ElseIf x=0 Thenr=2 End IfBIf x 0 Then r = 1 ElseIf (x = 0) Then r=2 Else r = 1 End IfCprint(IIf(x0,1,2)) Dr = IIf(x0,1,2)

  • 0~60~6

  • :,12002200~4002003%34004004%4500050005%

  • Visual Basic ForNext DoLoopDoDo

  • For to [Step ] [Exit For] Next =?TrueFalseFor-Next For-NextExit ForFor I=1 to 10 step 1 Print I Next i

  • 1=Int((-)/)+12l l l ()>0
  • : s=0For I=2 To 13 Step 3 s=s+i Next I Print s=; sPrint i=; i Int((13-2)/3)+1 : I=14

    Is12

  • ForN !NPrivate Sub Form_Click() Dim I%, f&, n% n = InputBox(, ) f = 1 For i = 1 To n f = f * I Next I Print n; "! ="; f End Sub

  • S=1+1/2+1/3++1/nnn =val(InputBox(n")) S = 0 For i = 1 To n S = S + 1/i Next i Print S 10-27382-76-124321-2589-20

  • ForNext For i=e1 To e2 [Step e3] For j=d1 to d2 [Step d3] .. Next j Next i (1) (2) (3)

  • Private Sub Form_Click()Dim i As Integer, j As IntegerFor i = 1 To 9 For j = 1 To i Print i; "*"; j; "="; i * j; Next j PrintNext iEnd SubPrint Tab((j - 1) * 13);

  • For j = 1 To 5 Print Tab(j); j For i = 1 To 6 Print "*"; Next i PrintNext j

  • Private Sub Form_Click() Dim a As Integer, i As Integer, j As Integer a = 0 For i = 1 To 2 For j = -2 To 2 a = a + i + j Next j Print a; Next i Print Print i, j, aEnd Sub

  • (1) X$=a : y$=b : z$=c For i=1 to 2 x$=y$ : y$=z$ : z$=x$ next I Print x$+y$+z$ End (2) For i=1 to 2 s=1 For j=0 to i-1 s=s+s*i Next j Print s; Next I End(3) For I=1 to 3 step 1 Print spc(5-I); For j=1 to 2*I-1 Print * Next j Print Next i For m=1 to 3 For n=0 to m-1 Next n Next m End

  • Do Loop Do1Do Loop While 2Do Loop Until 3Do While Loop 4Do Until Loop 1Do Loop While Do While Loop True Do Loop Until Do Until Loop True

  • YesNo1Do While [Exit Do] LoopYesNo2Do [Exit Do] Loop While x=1 Do Whilex
  • Do Until [Exit Do ] Loop34Do [Exit Do] Loop Until YesNoYesNox=1 Do Printx x=x+1 Loop Until x>=5

  • 50 ~ 1003Do4Private Sub Form_Click() x=50 do until x>100 if x mod 3 0 then print x, end if x=x+1 Loop End Sub

  • 1Do Until Loop A0B0C0D2x=1y=4Do Until y>4 x=x*y y=y+1LoopPrint xA) 1B) 4C) 8D) 20

  • 3Private Sub Command1_Click()x=0Do Until x=-1 a=InputBox(a) a=Val(a) b=InputBox (b) b=Val(b) x=InputBox (x) x=Val(x) a=a+b+xLoopPrint aEnd Sub54321-1A2B3C14D15

  • 4nametext1text2namecommand1Private Sub Command1_click()x=0do while x
  • 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 For I=1 to 9 print spc(30-3*I) for j=1 to I Print j; next j for k=I-1 to 1 step 1 Print k; next k print next i

  • 2.7 VB ()()

  • : Dim | Static | public(1[,2]) [As ] 60 : [ To ] 0 , : +1 : Dim kk(1 to 100) As IntegerDim A( 0 To 2 0 To 3) As Long A 34: Dim A(2 3) As Long34

    A(0,0)A(0,1)A(0,2)A(0,3)A(1,0)A(1,1)A(1,2)A(1,3)A(2,0)A(2,1)A(2,2)A(2,3)

  • (1) 0 ,1 Option Base 1 (2) n =Inputbox(n Dim x(n) As Single (3), Dim x(10) As Integer ' x11 x(10)=100 ' x(10)

  • (4)(5)AsVariantDim a(1 to 5) as integer Dim a(5) as integer Dim a(5, 1 to 5) as integer Option base 1 dim b(3,2) as integer

  • (6) Lbound([,]) Ubound([,]) Private Sub Form_Click() Dim a(34) As Integer Print LBound(a,1); UBound(a,1) Print LBound(a,2); UBound(a,2) End Sub

  • ()ReDim ReDim ([2]) [As ] Sub Form_Click( ) Dim s( ) As Single ReDim s(43 to 8) End Sub (1) DimPublicReDim (2) ReDim (3) ReDim

  • (4) ReDimReDimPreservePrivate Sub Form_Click() Dim a() As Integer n = 3 ReDim a(n) a(1) = 4 Print a(1) n = 5 ReDim a(n) ReDim preserve a(n) Print a(1) End Sub

  • dim s(5) as string s(1)=aa s(2)=s(1) & bb a=s(2) print s(1);s(2);a

  • ForInputBox dim a(5) as string for I=0 to 5 b=inputbox() a(I)=b next i dim a(4) as string a(0)=a : a(1)=b : a(2)=c : a(3)=d : a(4)=e 30

  • (1)For(Inputbox For i = 1 To 10 iA(i)=i Next i (2) Array =Array( Dim ib As Variant Dim ib() as Variantib = Array("abc", "def", "67") For i = 0 To UBound(ib) Print ib(i); " "; Next i (1)ArrayVariant (2)ArrayUbound

  • ForPrint1ForPrint a(5)a(5) For I=0 to 5 print a(I); next I print2For Print a(3,4) a(3,4)45For I=0 to 3 For j=0 to 4 print a(I,j); next j print next i

  • 1 dim a(3),b(2,3) a(0)=0:a(1)=1:a(2)=2:a(3)=3 b(1,3)=a(3) a(0)=b(1,3)2For dim a as variant,b() as variant a=array(1,2,3,4,5,6) redim b(ubound(a)) For i = 0 To ubound(a) b(i) = a(i) Print b(i); Next i

  • For Each Next for each In [ Exit For ] Next []dim a,va a=array(1,2,3,4,5) for each va in a print va; Next vavaa,

  • 110 PrivateSubCommand1_Click Dima(10)Asinteger,iAsInteger Fori=1To10 a(i)=InputBox() Nexti Fori=10To1step-1 Printa(i) NextI EndSub For i =1 To 10\2 t=A(i) A(i)=A(11-i) A(11-i)=t Next I Dim Max As Integer,iMax As Integer Max=A(1)n=1 For i = 2 To 10 If A(i)>Max Then Max=A(i) n=i End If Next I

  • 1(NameCommand1) Private Sub Command1_Click() Dim Arr1(10), arr2(10) As Integer n=3 For i = 1 To 5 Arr1(i) = i arr2(n) = 2 * n + i Next i Print arr2(n); Arr1(n) End Sub ( ) A.11 3B.3 11 C.13 3 D.3 13

  • 2(NameCommand1) Option Base 1 Private Sub Command1_Click() Dim a = Array(1, 2, 3, 4) j=1 For i = 4 To 1 Step -1 s = s + a(i)*j j =j * 10 Next i Print s End Sub ( )A.4321 B.12 C.34 D.1234

  • 3Option base 1Dim a as variant, max as integer, m as integer, sum as integerSum=0:max=0:m=0A=array(21,15,20,19,3,17)For i=1 to 6 sum=sum+a(i) if a(i)>max thenmax=a(i)m=I end ifNext IPrint max=;max, m=;m, sum=;sum

  • nPrivate Sub Command1_Click()Dim a(1 to 10) As Integern=10For i = 1 To n a(i) = InputBox("10") Print a(i);Next iPrintFor i = 1 To n-1 m = i For j = i + 1 To n If a(j) < a(m) Then m = j End If Next j t = a(i) a(i) = a(m) a(m) = tNext iPrint ""For i = 1 To n Print a(i);Next iPrintEnd Sub

  • (Index) Private Sub cmdName _Click(Index As Integer) If Index=3 then End If End Sub

  • 1105511029562A28764287025B79273241576678802+798+2725+8033n m410X1010

  • 510100 207 14.5077 15.1 156 14.2 231 14.7 453 15.2 276 13.9 096 15.7 122 13.7 339 14.9 302 14.5 1 2 3 10

  • Dim a As Variant, b As Varianta = Array("207", "156", "453", "096", "339", "077", "231", "276", "122", "302")b = Array(14.5, 14.2, 15.2, 15.7, 14.9, 15.1, 14.7, 13.9, 13.7, 14.5)For i = 0 To 8 Min = b(i) n = i For j = i + 1 To 9 If Min > b(j) Then Min = b(j) n = j End If Next j t = b(i) b(i) = b(n) b(n) = t t = a(i) a(i) = a(n) a(n) = tNext iPrint ; Tab(8); ; Tab(20); "For i = 0 To UBound(a) Print i + 1; Tab(8); a(i); Tab(20); b(i)Next i

  • 2.8 VB1VB2VBSubFunctionFunction

  • 1SubSub[Private|Public] [Static] Sub ([]) [] [Exit Sub] []End Sub SubPrivate Sub sumx as integery as integer dim s as integer s = x + y print ;sEnd SubSub

  • 1[Private|Public] Private2Static 3SubFunction 4Exit SubExit Sub

  • 5 6

  • 2Sub 12 3 End Sub

  • 3Sub SubEnd Sub1CallSubCall [] Call sumab2 []Call sum ab

  • Private Sub CmdOK() Call Swap(a,b) Swap a,bEnd SubPrivate Sub Swap(x%,y%) Dim temp% Temp=X X=Y Y=TempEnd Sub Sub

  • P55 12

  • Sub jchn%p&Dim i%p = 1For i = 1 To n p = p * iNext iEnd Sub n!7+11-10 Private Sub Form_Click Dim a&b&c&d& Call jch7a jch 11b Call jch10c d = a + bc Print "7!+11!-10!="d End Sub

  • 4Visual Basic 1 SubFunction 2 SubFunctionSubFunction A()

  • 3ByVal4ByRef

  • Sub Swap1(ByVal x%, ByVal y%)Dim Temp% Temp = x: x = y: y = TempEnd SubSub Swap2(x%, y%) Dim Temp% Temp = x: x = y: y = Temp End SubPrivate Sub Command1_Click() a% = 10: b% = 20: Swap1 a, b ' Print "A1="; a, "B1="; b a = 10: b = 20: Swap2 a, b ' Print "A2="; a, "B2="; bEnd Sub

  • Visual BasicStr( )val( )Function

  • 1 [Private|Public] [Static] Function ( [] ) [As ] [] [=] [Exit Function] []End Function 1AsAsVariant2=

  • myAbsPublic Function myAbs(a) as Single If a >= 0 Then ' myAbs = a ' 0 Else myAbs = -a ' End IfEnd Function2 ([]) SubPrivate Sub Command1_Click()Dim sr As Singlesr = InputBox("")Print "", myAbs(sr)End Sub

  • n!7+11-10Function jch(n%) as long Dim i%,f% f = 1 For i = 1 To n f = f * i Next I jch=fEnd Function Private Sub Form_ClickDim d as longd = jch(7)+ jch(11)jch(10)Print "7!+11!10!="dEnd Sub

  • Text1Command1Private Sub Command1_Click() n = Val(Text1.Text) If n\2 = n/2 Then f = f1(n) Else f = f2(n) End If Print f; nEnd SubPublic Function f1(ByRef x) x=x*x f1=x+xEnd FunctionPublic Function f2(ByVal x) x=x*x f2=x+x+xEnd Function6A) 7236 B) 10836 C) 726 D) 1086

  • (1) (2) (3)

  • 1 A) Sub Proc1ByVal nByVal m B) Sub Proc1nByVal m C) Sub Proc1nm D) Sub Proc1ByVal nm2 Sub Sx As Single,y As Single t = x x=t/y y=t Mod y End SubPrivate Sub Commandl_Click ( ) Dim a As Single Dim b As Single a =5:b =4 S a,b Print a,bEnd Sub A) 5 4 B) 1 1 C) 1.25 4 D) 1.25 1

  • 3 Function F(a As Integer) b = 0 Static c b = b+1 : c = c+1 f = a+b+c End Function A) 6 B) 4 C) 4 D) 6 6 5 6 7 6 6 8 8Private Sub Command1_Click() Dim a As Integer A=4 For I=1 To 3 Print F(a) Next IEnd Sub

  • 4 Public Sub Proc1(n As Integer,Byval m As Integer) n=n Mod 10 m=m\10 End Sub

    A) 12 34 B) 2 34 C) 2 3 D) 12 3Private Sub Command1_Click() Dim x As Integer,y As Integer x=12:y=34 Call Proc1(x,y) Print x,y End Sub

  • 11Function

  • 2.9 12345

  • CommandButton [] 1DefaultTrueCancelTrue

    Caption,, &Ok , OkDefaultCancelDefaultTrueCancelTruePicture(.bmp.Ico)Style1Style 0 , 1 ToolTipTextPictureDownPictureDisablePicture

  • 2 ClickMouseDownMouseUpKeyDownKeyUp3SetFocus .SetFocus

  • ()1 NameHeightWidthTopLeftEnabledVisibleFontForeColorBackColor21CaptionCaption2Alignment012

  • 3AutoSizeTrueFalse4BorderStyle015BackStyle013ClickDblClickChangCaption

  • 11Text TextText Caption2Maxlength , 03 MultiLine ,. False4ScrollBars MultiLineTrue,, 0-None1-Horizontal2-Vertical3-Both

  • 5Locked False6PassWordChar PasswordChar MultiLine False7SelStartSelLengthSelText SelStart 0SelStartSelStart SelLength SelText

  • Private Sub Command1_Click() Text1.SelStart = 0 Text1.SelLength = Len(Text1.Text) Print Text1.SelTextEnd Sub0123456789 SelStart=5,SelLength=4, SelText="5678" 2SetFocus

  • 31ChangeTextTextChange Private Sub Text1_Change() Static x As Integer x = x + 1 Text2.Text = Str(x) End Sub2GotFocusTabSetFocusText Private Sub Text1_GotFocus() Text1.SelStart=0 Text1.SelLength=Len(Text1.Text) End Sub

  • 3KeyPress ANSIKeyAsciiAscii Private Sub Text1_KeyPress(KeyAscii As Integer) Print KeyAscii, Chr(KeyAscii) End Sub ABCD4LostFocusTabLostFocus Private Sub Text1_LostFocus() If Text1.Text = Then MsgBox Text1.SetFocus End If End Sub

  • Tab Text1_ LostFocus :Tab, IsNumeric(Text1) : . Text1.SetFocus :

  • ScrollBarHScrollbarVscrollBar

  • 1. (1) Value (2) Max -32,768~32,767(3) Min -32,768~32,767(4) SmallChange (5) LargeChange

  • 2 1Change ChangeValueChange2Scroll ScrollScroll ScrollChangeScrollChange

  • P34

  • 10 ~ 100

  • 2.10 2.11

  • 2.8 ListBox List1List2List31List List00 List1.List(0)=" List1.List(1)=" List1.List(2)=" List1.List(3)=" List1.List(4)="

  • (2)ListIndexListIndexListIndex-1 (3)ListCountListCountListCount-10~ListCount-1List(4)SortedSortedTrueFalse

  • (5)TextTextList1.TextList1.ListIndex(6) MultiSelect MultiSelect

    012 SHIFT CTRL

  • 7SelectedList List1.Selected0=TrueList1ListIndex0List1.selected(0)=trueList1.selected(2)=trueList1.selected(1)=falseList1.selected(3)=falseList1.SelCount=28SelCount SelCount SelCount 0

  • (9)Style01List1.List(0)=List1.List(1)= List1.ListIndex = 3 (0) List1. ListCount = 5 List1. Selected(3) = TrueFalse List1. Sorted = False List1. Text List1. List(List1. ListIndex)

  • ClickDblClickListAddItemRemoveItemClear(1)AddItem .AddItem ,AddItem0ListCount-1

  • (2)RemoveItem .RemoveItem List1Command1Command2 Private Sub Command1_Click List1.AddItem "One"0 End Sub Private Sub Command2_Click List1.RemoveItem 0 End Sub(3)Clear .ClearClearListCount0

    (Year mod 4=0) and (year mod 100 0) or (year mod 400=0)A0 and b*b-4*a*c>=0(Sex=true and age>=18 and age=1.65) or (sex=false and age>=16 and age=1.60)Marrigerat=true and age>=26 and workingage>=5