输入半径,计算圆周长和圆面积(保留小数点后两位小数)
进一步要求,为了保证程序运行正确,对输入的半径要进行合法性检查。数据输入结束有两种方法,
分别编事件过程对数据进行检验:
1:按Tab键,检查数据的合法性,利用TextBox1_LostFocus事件。
2:按回车键,当TextBox1_KeyPress事件中的返回参数e.KeyChar的ASC()值为13时表示输入结束。
Public Class Form1 Private Sub TextBox1_KeyPress(ByVal sender As Object,ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If Asc(e.KeyChar) = 13 Then If Not IsNumeric(TextBox1.Text) Then MsgBox("输入数字有非数字字符,请重新输入","数据检验") TextBox1.Text = "" TextBox1.Focus() End If End If End Sub Private Sub TextBox1_LostFocus(ByVal sender As Object,ByVal e As System.EventArgs) Handles TextBox1.LostFocus If Not IsNumeric(TextBox1.Text) Then MsgBox("输入数字有非数字字符,请重新输入","数据检验") TextBox1.Text = "" TextBox1.Focus() End If End Sub Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click Dim r!,c! r = TextBox1.Text c = 2 * 3.1415926 * r TextBox2.Text = Format(c,"0.00") End Sub Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button2.Click Dim r!,s! r = TextBox1.Text s = 3.1415926 * r * r TextBox3.Text = Format(s,"0.00") End Sub End Class原文链接:https://www.f2er.com/vb/257648.html