vb 中如何取一个文本框中从第几行到第几行的数据?

需要赋值给数组。
为啥出现Invalid ReDim呢?

第1个回答  2007-12-12
读取Text指定行号内容
'添加 Text1 Text2 Command1
'Text1 Multiline属性要设为 True
'Text2 中输入你要读取第几行的内容

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub RtlMoveMemory Lib "KERNEL32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
Const EM_GETLINE = &HC4
Const EM_LINEINDEX = &HBB
Const EM_LINELENGTH = &HC1
Dim a$, fname$
Private Sub Form_Load()
fname = "c:\axleop.txt"
Open fname For Input As #1
Text1.Text = ""
While Not EOF(1)
Line Input #1, a
Text1.Text = Text1.Text & a & vbCrLf
Wend
Close #1
End Sub

Private Sub Command1_Click()
If Val(Text2.Text) > 0 Then
MsgBox GetLine(Text1, Val(Text2.Text) - 1)
End If
End Sub

Function GetLine(txt As TextBox, ByVal Line As Integer) As String
Dim S As String, Length As Integer, pos As Long
GetLine = ""
pos = SendMessage(txt.hwnd, EM_LINEINDEX, Line, ByVal 0&)
Length = SendMessage(txt.hwnd, EM_LINELENGTH, pos, ByVal 0&)
S = String(Length, Chr(0))
RtlMoveMemory ByVal S, Length, 2
If SendMessage(Text1.hwnd, EM_GETLINE, Line, ByVal S) > 0 Then
GetLine = S
End If
End Function

参考资料:http://hi.baidu.com/cbm666/blog/item/9d224316e157c84b21a4e934.html

第2个回答  2007-12-12
a=split(text1,vbcrlf)

然后数组a中, a(0)是第1行, a(1)是第2行, 以下类推..

如果你想要第5行到第17行, 可以使用:
a=split(text1,vbcrlf)
redim preserve a(4 to 16)
b=join(a,vbcrlf)本回答被提问者采纳