DOCX

# VB

By Leon Reed,2014-06-29 12:45
29 views 0
VB

1、二元一次方程

Dim a%, b%, c%, p!, x1, x2 Private Sub Command1_Click() a = Val(Text1)

b = Val(Text2)

c = Val(Text3)

p = Format(b * b - 4 * a * c, "0.00")

If p >= 0 Then

x1 = (-b + Sqr(p)) / 2 * a

x2 = (-b - Sqr(p)) / 2 * a

Text4 = Format(x1, "0.00")

Text5 = Format(x2, "0.00")

'Label6.Caption = ""

Else

x1 = -b / (2 * a) & "+" & Sqr(-p) / (2 * a) & "i"

x2 = Format(-b / 2 * a, "0.00") & "-" & Format(Sqr(-p) / 2 * a, "0.00") & "i"

'Label6.Caption = "判别式小于0，请重输入系数"

'Text1 = ""

'Text2 = ""

'Text3 = ""

'Text1.SetFocus

Text4 = x1

Text5 = x2

End If

End Sub

Private Sub Command2_Click() Text1 = ""

Text2 = ""

Text3 = ""

Text4 = ""

Text5 = ""

End Sub

Private Sub Command3_Click() End

End Sub

2、温度转换

Dim f!, c!

Private Sub Command1_Click() f = Val(Text1)

c = 5 * (f - 32) / 9

Text2 = Format(c, "0.00") End Sub

Private Sub Command2_Click() c = Val(Text2)

f = 9 * c / 5 + 32

Text1 = Format(f, "0.00") End Sub

Private Sub Form_Click() Text1 = ""

Text2 = ""

End Sub

3、圆面积

Dim r!, s!, c!

Private Sub Command1_Click() r = Val(Text1)

s = 3.14 * r * r

Text2 = s

End Sub

Private Sub Command2_Click() r = Val(Text1)

c = 3.14 * 2 * r

Text3 = c

End Sub

4、查找和替换

Private Sub Command1_Click() i = InStr(Text1, Text2) k = i + Len(Text2)

ls = Left(Text1, i - 1) Text4 = ls + Text3 + Mid(Text1, k)

End Sub

Private Sub Command2_Click() Text4 = Replace(Text1, Text2, Text3)

End Sub

Private Sub Command3_Click() i = Shell("calc.exe", 1) End Sub

Private Sub Command4_Click() Text1 = ""

Text2 = ""

Text3 = ""

Text4 = ""

End Sub

5、随机函数

Dim k%

Private Sub Form_Click()

Randomize

k = Int(10 * Rnd + 1)

Print k;

End Sub

6KeyPress事件

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then

Label1.Caption = Text1.Text

End If

End Sub

7、人民币和美元兑换

Private Sub Command1_Click()

Text1 = ""

Text2 = ""

Text3 = ""

End Sub

Private Sub Command2_Click()

If Text2 = "" Then

Label4 = "请输入兑换比率,且必须为数字型"

Text2 = ""

Text2.SetFocus

Else

Text3 = Val(Text1) / Val(Text2)

End If

End Sub

Private Sub Command3_Click()

Text1 = Val(Text3) * Val(Text2)

End Sub

8、每行逐个增加一个?

Dim i%

Private Sub Form_Click()

i = i + 1

Print Tab(9 - i); String(i, "?")

9、空三角

Private Sub Form_Click()

Print Tab(9); String(1, "*")

Print Tab(8); String(1, "*"); Space(1); String(1, "*") Print Tab(7); String(1, "*"); Space(3); String(1, "*") Print Tab(6); String(1, "*"); Space(5); String(1, "*") Print Tab(5); String(1, "*"); Space(7); String(1, "*") Print Tab(4); String(11, "*")

End Sub

10、列表

Private Sub Command1_Click()

Label1.Caption = List1.Text

End Sub

Private Sub Command2_Click()

Label2.Caption = List1.ListCount

End Sub

Private Sub Command3_Click()

Label3.Caption = List1.ListIndex

End Sub

11、小写转换成大写

Dim aa As String * 1

aa = Chr\$(KeyAscii)

Select Case aa

Case "A" To "Z"

aa = Chr\$(KeyAscii + 32)

Case "a" To "z"

aa = Chr\$(KeyAscii - 32)

Case " "

Case Else

aa = "*"

End Select

Text2.Text = Text2.Text & aa

12、分类统计

Dim a(1 To 26) As Integer, c As String * 1

le = Len(Text1)

For i = 1 To le

c = UCase(Mid(Text1, i, 1))

If c >= "A" And c <= "Z" Then

j = Asc(c) - 65 + 1

a(j) = a(j) + 1

End If

Next i

For j = 1 To 26

If a(j) > 0 Then Picture1.Print ""; Chr\$(j + 64); "="; a(j);

Next j

13、字体效果

Private Sub Combo1_Click() Label4.FontName = Combo1.Text End Sub

Private Sub Combo2_Click() Label4.FontSize = Combo2.Text End Sub

Private Sub Combo2_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then

Label4.FontSize = Combo2.Text End If

End Sub

For i = 1 To Screen.FontCount - 1

For i = 6 To 40 Step 2

Next i

End Sub

14、随机产生10个数字，求最值和平均

Dim a(1 To 10) As Integer, max%, min%, avg!, sum%, i%

Private Sub Form_Click()

Randomize

For i = 1 To 10

a(i) = Int(71 * Rnd + 30)

sum = sum + a(i)

Print a(i);

Next i

max = a(1): min = a(1)

For i = 1 To 10

If min > a(i) Then min = a(i)

If max < a(i) Then max = a(i) Next i

Print

Print "max="; max; "min="; min; "avg="; sum / 10

End Sub

15、数组输出选定并显示下标 Private Sub Command1_Click()

Label1.Caption = List1.Text & "下标" & List1.ListIndex End Sub

16、知道6个学生成绩，通过对数组赋值的方法，利用string函数，以没5分为一个"?"

Dim a(1 To 6) As Single

Private Sub Form_Click()

For i = 1 To 6

a(i) = InputBox("输入" & "a(" & i & ")的值")

Print String(Int(a(i) / 5), "?"); a(i)

Next i

End Sub

17、随机产生20个数，并统计个分数段人数

Dim a%(1 To 20), s%(9), j%, k%

Private Sub Command1_Click()

Randomize

For i = 1 To 20

j = j + 1

a(i) = Int(101 * Rnd + 0)

Picture1.Print Tab(5 * j - 4); a(i);

If j > 4 Then

j = 0

Picture1.Print

End If

Next i

End Sub

Private Sub Command2_Click()

For i = 1 To 20

k = Int(a(i) / 10)

s(k) = s(k) + 1

Next i

For k = 0 To 9

Picture2.Print k * 10 & "" & (k + 1) * 10 & "分数阶段的人数" & s(k)

Next k

End Sub

18、选课程并在右边list显示，多与5门提示

Private Sub List1_Click()

If List2.ListCount < 5 Then

List1.RemoveItem List1.ListIndex

Else

MsgBox "超过5门课程，不能再选" End If

End Sub

19.、月工资计算

Private Sub Form_Click()

Dim sfgz, jbgz, yye As Single

jbgz = Val(InputBox("请输入基本工资", "基本工资输入框", 300))

yye = Val(InputBox("请输入本月营业额", "营业额输入框"))

sfgz = jbgz + yye * 0.05

Print "本月营业额为："; yye; "基本工资为："; jbgz

Print "本月实发工资为："; sfgz End Sub

20、登陆，密码错误三次不允许输入 Dim k As Integer

Dim i As Integer

Private Sub Command1_Click()

If k < 2 Then

If Text2.Text <> "hong" Then

i = MsgBox("密码错误", 5 + vbExclamation, "输入密码")

If i = 2 Then

End

Else

Text2.Text = ""

Text2.SetFocus

End If

Else

MsgBox "欢迎登陆"

End If

Else

Text2.Enabled = False

MsgBox "登陆失败"

End If

k = k + 1

End Sub

Private Sub Text1_LostFocus() If Not IsNumeric(Text1) Then

MsgBox "账号有非法字符错误", 0, "输入账号"

Text1.Text = ""

Text1.SetFocus

End If

21、购物打折

If KeyAscii = 13 Then

x = Text1

If x >= 3000 Then

y = 0.7 * x

ElseIf x >= 2000 Then

y = 0.8 * x

ElseIf x >= 1000 Then

y = 0.9 * x

Else

y = x

End If

Text2 = y

End If

22、随机计算题，并计分

Dim result!, nok%, nerror% Private Sub Command1_Click() Label1 = ""

Picture1.Print "_________________" Picture1.Print "一共计算" & (nok + nerror) & "道题"

Picture1.Print "得分" & Int(nok / (nok + nerror) * 100)

End Sub

Dim num1%, num2%, nop%, op\$ Randomize

num1 = Int(10 * Rnd + 1)

num2 = Int(10 * Rnd + 1)

nop = Int(4 * Rnd + 1)

Select Case nop

Case 1

op = "+": result = num1 + num2 Case 2

op = "-": result = num1 - num2 Case 3

op = "×": result = num1 * num2 Case 4

op = "?": result = num1 / num2 End Select

Label1 = num1 & op & num2 & "=" End Sub

Private Sub Picture1_Click()

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then

If Val(Trim(Text1)) = result Then Picture1.Print Label1; Text1; Tab(10); "?"

nok = nok + 1

Else

Picture1.Print Label1; Text1; Tab(10); "×"

nerror = nerror + 1

End If

Text1 = ""

Text1.SetFocus

End If

End Sub

23、上网计费

Dim x, m, y@

Private Sub Form_Click()

m = InputBox("输入上网时的时间", "计费")

x = DateDiff("h", m, Now)

If x < 10 Then

y = 30

ElseIf x >= 10 And x < 50 Then

y = (x - 10) * 2.5 + 30

ElseIf x >= 50 Then

y = (x - 50) * 2 + (50 - 10) * 2.5 + 30

End If

If y >= 150 Then

y = 150

End If

MsgBox "上网费用为" & y, , "计费"

24、使用print方法输出图形

Private Sub Form_Click()

Print

For i = 1 To 5

Print Tab(i); String(6 - i, "?"); Space(6); String(i, "?")

Next i

End Sub

25、输出等腰三角形 Private Sub Form_DblClick()

For i = 1 To 5

Print Tab(9 - i); String(2 * i - 1, "*")

Next i

End Sub

26、输出图形，实验D1

1

222

33333

4444444

555555555

。。。。。。。。 Dim i%

Private Sub Form_Click()

For i = 1 To 9

Print Tab(20 - i); String(2 * i - 1, Trim(Str(i)))

Next i

End Sub

27、计算s=1+1/2+1/4+1/7+1/11+.当第i项的值<10

-4时结束

Dim i%

Private Sub Form_Click()

For i = 1 To 9

Print Tab(20 - i); String(2 * i - 1, Trim(Str(i)))

Next i

End Sub

28、统计数字 Dim n&

Private Sub Command1_Click()

If Text1 < 9 Then

n = 9

Report this document

For any questions or suggestions please email
cust-service@docsford.com