求教。。。连加

来源:百度知道 编辑:UC知道 时间:2024/06/22 15:00:38
题目是:
1+2+3+4+……+a=?
a为任意在textbox中输入的数
结果输出在另一个textbox中

Private Sub Form_Load()
Text1.Text = ""
Text2.Text = ""
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 And Text1.Text <> "" And IsNumeric(Text1.Text) Then
Text2.Text = Val(Text2.Text) + Val(Text1.Text)
End If
End Sub

按回车确认输入

Private Sub Command1_Click()
Dim a As Integer
a = Val(Text1.Text)
s = 0
For i = 1 To a
s = s + i
Next i
Text2.Text = s
End Sub

Private Sub Form_Click()
Dim i%, sum%, a%
a = Val(Text1.Text)
For i = 1 To a
sum = sum + i
Next i
Text2.Text = CStr(sum)
End Sub
单击窗体