vb text相加

来源:百度知道 编辑:UC知道 时间:2024/06/24 09:36:36
控件:text1,text2,text3,command1
在text1里面输入一组数字:12 85 74 69 73 91 65 (共七个数)
在text2里面输入另外一组字符串:第一个数+第二个数+第五个数 ……让它们相加.
怎样按command1在text3里面得到它们相加的结果。
{"第一个数"就是指text1里面的12
"第二个数"就是指text1里面的85
"第三个数"就是指text1里面的74
"第四个数"就是指text1里面的69……}
谢谢各位大侠!

恩 你看看,在Command1中写下面代码就是

Private Sub Command1_Click()
Dim a, b, c
c = 0
a = Text1.Text
For i = 1 To 7
b = Mid(a, 3 * i - 2, 2)
c = c + b
If i = 7 Then
Text2.SelText = "第" & i & "个数"
Else
Text2.SelText = "第" & i & "个数" & "+"
End If
Print b & ":" & c
Next i
Text3.Text = "=" & c
End Sub

但是这里我要说明一下,输入数字的格式必须是像你上面那样(xx xx xx xx xx xx xx)每个数只能是两位这里只加了7位,如果还要多输入按照同样的方法改进就是

Option Explicit

Private Sub Command1_Click()
Dim i As Long, n1() As String, n2() As String, tmpstr As String
n1 = Split(Text1.Text, " ")
n2 = Split(Text2.Text, " ")
If UBound(n1) <> UBound(n2) Then
MsgBox "两次输入的数字数目不相同!", vbCritical + vbOKOnly, "Error"
Else
For i = 0 To UBound(n1)
tmpstr = tmpstr & CStr(Val(n1(i)) + Val(n2(i))) & " &quo