vb编程 输入圆半径,计算圆周长和圆面积

来源:百度知道 编辑:UC知道 时间:2024/05/02 05:50:43
输入圆半径,计算圆周长和圆面积
为了保证程序运行的正确,对输入的半径要进行合法性检查,如发现输入的数中有非法数字,利用msgbox显示出错信息,利用setfocus方法定位出错的文本框处,重新输入

Private Sub cmdOK_Click()
Dim PI As Double
PI = 3.14

If IsNumeric(Trim(txtR.Text)) = False Then
MsgBox "请输入一个合法的数"
txtR.Text = ""
txtR.SetFocus
Exit Sub
End If
txtG.Text = FormatNumber((2 * PI * Val(txtR.Text)), 2)
txtA.Text = FormatNumber(PI * Val(txtR.Text) * Val(txtR.Text), 2)

End Sub

Private Sub Form_Load()
txtR.Text = ""
txtG.Text = ""
txtA.Text = ""
Me.Show
txtR.SetFocus
End Sub
-----------------
txtR:半径 txtG:周长 txtA: 面积

Private Sub Command1_Click()
Dim pi As Single
Dim r As Single
Dim L As Single
Dim A As Single
pi = 3.141592654
r = InputBox("输入半径R=")
L = pi * r
A = r ^ 2 * pi / 4
Print "周长=", L
Print "面积=", A
End Sub