当前位置:文档之家› VB实验指导例题

VB实验指导例题

VB实验指导例题
VB实验指导例题

实验A VB环境和可视化编程基础

1.

(1)运行界面

(2)参考代码:

Private Sub Command1_Click()

label3.Caption = text1.Text

End Sub

2.模仿教材例1.1,将事件过程中自上而下移动改为自右向左移动,也要考虑文字出窗体边界的情况。

(1)运行界面

(2)参考代码:

Private Sub Command1_Click()

Timer1.Interval = 0

Call mymove

End Sub

Sub mymove()

Label1.Move Label1.Left - 20

If Label1.Left = 0 Then Label1.Left = Form1.Width

End Sub

Private Sub Command2_Click()

Timer1.Interval = 200

End Sub

Private Sub Timer1_Timer()

Call mymove

End Sub

3.编写一程序,在文本框中统计在该窗口上鼠标单击的次数。(1)运行界面:

(2)编写代码:

Private Sub Form_Click()

Text1 = Val(Text1) + 1

End Sub

Private Sub Form_Load()

Text1.Text = ""

End Sub

4.代码:

Private Sub Form_Click()

Form1.Caption = "鼠标单击"

Form1.Picture = LoadPicture("d:\1.jpg")

End Sub

Private Sub Form_DblClick()

Form1.Caption = "鼠标双击"

Form1.Picture = LoadPicture("d:\2.jpg")

End Sub

Private Sub Form_Load()

Form1.Picture = LoadPicture("d:\3.jpg")

End Sub

5.命令按钮、字号、容和格式的复制练习(1)运行界面

(2)参考代码:

Private Sub Command1_Click()

Text1.FontName = "黑体"

Text1.FontSize = 25

End Sub

Private Sub Command2_Click()

Text2.Text = Text1.SelText

Text2.FontName = Text1.FontName

Text2.FontSize = Text1.FontSize

Text1.SetFocus

End Sub

实验B 顺序结构

1.编一个华氏温度与摄氏温度之间转换的程序,要求用按钮实现转换。既单击“华氏转摄氏”按钮,则将华氏温度转换为摄氏温度;同样,单击“摄氏转华氏”按钮,则将摄氏温度转换为华氏温度。

代码:

Private Sub Command1_Click()

Dim f!, c!

f = Val(Text1.Text)

c = 5 / 9 * (f - 32)

Text2.Text = Format(c, "0.00")

End Sub

Private Sub Command2_Click()

Dim f!, c!

c = Val(Text2.Text)

f = 9 / 5 * c + 32

Text1.Text = Format(f, "0.00")

End Sub

2.(1)运行界面

(2)参考代码

Private Sub Command1_Click()

r = Text1.Text

Label1.Caption = Format(3.14 * r * r, ".00") Text1.SetFocus

End Sub

Private Sub Command2_Click()

r = Val(Text1.Text)

Label2.Caption = Format(3.14 * 2 * r, ".00") Text1.SetFocus

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then

If Not IsNumeric(Text1) Then

MsgBox "输入有误,请重输入"

Text1.SetFocus

Text1.SelStart = 0

Text1.SelLength = Len(Text1.Text)

End If

End If

End Sub

Private Sub Text1_LostFocus()

If Not IsNumeric(Text1) Then

MsgBox "输入有非数字字符,请重新输入", , "2-2" Text1.SetFocus

Text1.SelStart = 0

Text1.SelLength = Len(Text1.Text)

End If

End Sub

3.我国有13亿人口,按人口增长0.8%计算,多少年后我国人口超过26亿。

(1)运行界面

(2)参考代码

Private Sub Command1_Click()

a = Int(Log(2) / (Log(1 + 0.008))) + 1

Text1.Text = a & "年后,我国人口超过26亿"

End Sub

4.输入一字符串,分别调用Ucase、Len函数将其转换成大写字母并显示其字符串长度(1)运行界面

(2)参考代码

Private Sub Command1_Click()

Text2.Text = UCase(Text1.Text)

Text3.Text = Len(Text1.Text)

End Sub

5.随机产生一个3位正整数,然后逆序输出,产生的随机数与逆序数同时显示。例如,产生735,输出是537

(1)运行界面

(2)参考代码:

Private Sub Command1_Click()

x = Int((899 + 1) * Rnd + 100)

a = x \ 100

b = (x Mod 100) \ 10

c = x Mo

d 10

Text1.Text = x

Text2.Text = c * 100 + b * 10 + a

End Sub

6.使用Mid、Left、Right函数。在Text1文本框中输入一字符串,在label1、Label2、Label3中显示如图的效果。

(1)运行界面

相关主题
文本预览
相关文档 最新文档