(2)参考代码
Private Sub Command1_Click() r = Text1.Text
Label1.Caption = Format(3.14 * r * r, \Text1.SetFocus End Sub
Private Sub Command2_Click() r = Val(Text1.Text)
Label2.Caption = Format(3.14 * 2 * r, \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 \输入有非数字字符,请重新输入\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 Mod 10 Text1.Text = x
Text2.Text = c * 100 + b * 10 + a End Sub
6. 使用Mid、Left、Right函数。在Text1文本框中输入一字符串,在label1、Label2、Label3
中显示如图的效果。 (1) 运行界面
(2)参考代码:
Private Sub Form_Click() Text1.FontSize = 16 Label1.FontSize = 16 Label2.FontSize = 16 Label3.FontSize = 16
Text1.Text = \程序设计教程(第三版)\Label1.Caption = Left(Text1.Text, 11) Label2.Caption = Mid(Text1.Text, 12, 6) Label3.Caption = Right(Text1.Text, 5) End Sub
7. Print方法练习,显示字符图形。 (1) 运行界面
(2)参考代码: (a)
Private Sub Command1_Click() Print
For i = 1 To 5
Print Tab(13 - 2 * i); String(2 * i - 1, \★\★\Next i End Sub
Private Sub Command2_Click() Form1.Cls End Sub (b)
Private Sub Command1_Click() Print
For i = 1 To 5
Print Tab(13 - 2 * i); String(2 * i - 1, \★\☆\★\Next i End Sub
Private Sub Command2_Click()
相关推荐: