用VB编程高手进

来源:百度知道 编辑:UC知道 时间:2024/05/26 08:11:04
文本框输入一个数n,若此数是非负整数,则求s=1*3*5..*(2n+1),否则选中文本,直到输入整数为止

Private Sub Command1_Click()
Sum = 1
If IsNumeric(Text1.Text) Then '判断文本框输入的是不是数字
If Abs(CLng(Text1.Text)) = CDbl(Text1.Text) Then '判断是不是非负整数
If CLng(Text1.Text) > 0 Then '判断是不是0
For i = 1 To CLng(Text1.Text) '计算
Sum = Sum * (2 * i - 1)
Next
MsgBox Sum
Else '输入的是0
MsgBox "请输入正整数"
Text1.SetFocus '设置焦点
Text1.SelStart = 0 '选中文本
Text1.SelLength = Len(Text1.Text)
End If
Else '输入的是小数或者负数
MsgBox "请输入正整数"
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
End If
Else '输入的不是数字
MsgBox "请输入正整数"
Text1.SetFocus