vb未知文本框内容数量,怎么将文本框内容赋值给变量?数组分割后存入变量的方法
问题请教下各位,文本框的坐标文本,
例如:111,222回车333,444回车555,666,这样的格式不确定有多少组
怎样才能不下标越界,请教高手优化代码,或给方法,我是新手,请大家多多关照
Private Sub Command1_Click()
Dim linestring() As String
Dim temp1() As String
Dim temp2() As String
Dim temp3() As String
Dim temp4() As String
Dim temp5() As String
linestring = Split(Text1.Text, Chr(13))
temp1() = Split(linestring(0), ",")
temp2() = Split(linestring(1), ",")
temp3() = Split(linestring(2), ",")
temp4() = Split(linestring(3), ",")
temp5() = Split(linestring(4), ",")
For i = 0 To UBound(temp1)
Print "数组1的第" & i + 1 & "项是: " & temp1(i)
Print "数组2的第" & i + 1 & "项是: " & temp2(i)
Print "数组3的第" & i + 1 & "项是: " & temp3(i)
Print "数组4的第" & i + 1 & "项是: " & temp4(i)
Print "数组5的第" & i + 1 & "项是: " & temp5(i)
Next i
End Sub vb 优化 数组分割后存入变量
[解决办法]
Option Explicit
'Option Base 0
Private Sub Command1_Click()
Dim linestring() As String
Dim strCoord() As String
Dim i&, p&, m&, strTemp$
strTemp = Text1.Text
linestring = Split(strTemp, vbCrLf)
m = UBound(linestring)
' 坐标组数 = m+1
' 下面这句,如果最后一行结尾是空行,则去掉它
If (Right$(strTemp, 2) = vbCrLf) Then m = m - 1
ReDim strCoord(1, m)
For i = 0 To m
strTemp = linestring(i)
p = InStr(strTemp, ",")
strCoord(0, i) = Left$(strTemp, p - 1)
strCoord(1, i) = Mid$(strTemp, p + 1)
Next
For i = 0 To m
Print "第 " & i + 1 & " 组坐标: X = " & _
strCoord(0, i) & " Y= " & strCoord(1, i)
Next
End Sub