VB实验指导例题要点
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验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