VB考试常见编程题型
% 整型 -32768~32767整数
& 长整型 -2147483648~2147483647整数 &、&O八进制 &H十六进制! 单精度型指数符E eg:1.23E-5
# 双精度型指数符D eg:1.23D4
$ 字符串型
随即改变背景颜色:
Private Sub Form_Click()
r1 = Int(Rnd() * 256)
g1 = Int(Rnd() * 256)
b1 = Int(Rnd() * 256)
Form1.BackColor = RGB(r1, g1, b1)
End Sub
改变背景:
Private Sub Command1_Click()
Form1.Picture = LoadPicture("E:\镜音\5f1f0547c9229a6ccefca3d0.jpg") End Sub
窗体1、窗体2互相显示隐藏(需2个窗体)
窗体1:
Private Sub Form_Click()
Form1.Hide
Form2.Show
End Sub
窗体2:
Private Sub Form_Click()
Form2.Hide
Form1.Show
End Sub
计算水仙花数(P78)
判断素数(P79)
计算Fibonacci数列(P79)
输入一个整数,计算其各位数字和(P81)
Private Sub Form_Click()
Dim x&, y&, i&
i = InputBox("shuruzhengshu")
x = 0
While i <> 0
y = i Mod 10
x = x + y
i = i \ 10
Wend
Print x
End Sub
输入两个整数,计算最大公约数(P82)
Private Sub Form_Click()
Dim a%, b%, c%
a = InputBox("1st")
b = InputBox("2nd")
c = a
While a Mod c <> 0 Or b Mod c <> 0
c = c - 1
Wend
MsgBox (a & "he" & b & "zuidagongyueshushi" & c) End Sub
输入两个整数,计算最小公倍数
Private Sub Form_Click()
Dim x&, y&, z&
x = InputBox("1st")
y = InputBox("2nd")
z = IIf(x > y, x, y)
While z Mod x <> 0 Or z Mod y <> 0
z = z + 1
Wend
MsgBox (x & "he" & y & "dezuixiaogongbeishushi" & z) End Sub
利用滚动条改变图片框中背景的颜色。(P116)
Private Sub Form_Load()
Slider1.Min = 0
Slider1.Max = 255
Slider1.Value = 0
https://www.360docs.net/doc/bc3992488.html,rgeChange = 25
Slider2.Min = 0
Slider2.Max = 255
Slider2.Value = 0
https://www.360docs.net/doc/bc3992488.html,rgeChange = 25
Slider3.Min = 0
Slider3.Max = 255
Slider3.Value = 0
https://www.360docs.net/doc/bc3992488.html,rgeChange = 25
End Sub
Private Sub Slider1_Click()
red = Slider1.Value
Text1.BackColor = RGB(red, green, blue)
Label1 = "r:g:b=" & Slider1.Value & ":" & Slider2.Value & ":" & Slider3.Value End Sub
Private Sub Slider2_Click()
green = Slider1.Value
Text1.BackColor = RGB(red, green, blue)
Label1 = "r:g:b=" & Slider1.Value & ":" & Slider2.Value & ":" & Slider3.Value End Sub
Private Sub Slider3_Click()
blue = Slider1.Value
Text1.BackColor = RGB(red, green, blue)
Label1 = "r:g:b=" & Slider1.Value & ":" & Slider2.Value & ":" & Slider3.Value End Sub
倒计时(P125)
Dim i As Integer
Private Sub Form_Load()
Timer1.Interval = 1000
Timer1.Enabled = False
End Sub
Private Sub Command1_Click()
i = 10
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
i = i - 1
Label1 = "nidekaoshishijianhaiyou" & i & "miao"
If i = 0 Then End
End Sub
动态数组使用示例(P136)
Dim a() As Integer
Private Sub Form_Click()
Print "=====1st====="
ReDim a(5) As Integer
For i = 0 To 5
a(i) = i * 2
Next i
For i = 0 To 5
Print a(i);
Next i
Print "=====2nd====="
ReDim Preserve a(4) As Integer
For i = 0 To 4
Print a(i);
Next i
Print "=====3rd====="
ReDim a(6) As Integer
For i = 0 To 6
Print a(i);
Next i
End Sub
使用ADO Data和DataGrid控件连接数据库,并显示其中的一个表。(P211) Adodc1:
CommandType: adCmdT able
ConnectString:Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=D:\VBasic\00000001\books.mdb;Persist Security Info=False RecordSource: select 书号,书名,书价,数量from book
DataGrid1:
DataSource: Adodc1
按书号查询(P220)
Private Sub Command2_Click()
Dim str$
str = "书号='" & Trim(Text4) & "'"
Adodc1.Recordset.MoveFirst
Adodc1.Recordset.Find (str)
If Adodc1.Recordset.EOF Then
Adodc1.Recordset.MoveFirst
MsgBox ("No Found!")
End If
End Sub
移动记录(P221)
Private Sub Command1_Click()
Adodc1.Recordset.MovePrevious
If Adodc1.Recordset.BOF Then MsgBox "已到首记录"
End Sub
Private Sub Command2_Click()
Adodc1.Recordset.MoveNext
If Adodc1.Recordset.EOF Then MsgBox "已到尾记录"
End Sub
Adodc1.Recordset.MoveFirst
Adodc1.Recordset.MoveLast
使用DataGrid控件选择显示部分字段(P224)
Adodc1:
CommandType: adCmdT able
ConnectString: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\VBasic\00000001\books.mdb;Persist Security Info=False
RecordSource: book
Text1:
DataSource: Adodc1
DataField: 书名
Text2:
DataSource: Adodc1
DataField: 书价
Text3:
DataSource: Adodc1
DataField: 金额
计算金额,金额=数量*单价(P228)
Private Sub Command1_Click()
Adodc1.Recordset.MoveFirst
Do While Not Adodc1.Recordset.EOF
Adodc1.Recordset.Fields("金额") = Adodc1.Recordset.Fields("书价") * Adodc1.Recordset.Fields("数量")
Adodc1.Recordset.MoveNext
Loop
Adodc1.Recordset.MoveFirst
End Sub