-
(Resolved) How to read each line from multiline textbox?
Hey All,
I use the following code to read each line from a text file (.txt).
VB Code:
Private Const NUMOFTXTBOXES = 6
Private Sub Command1_Click()
Dim strArray(0 To NUMOFTXTBOXES - 1) As String
Dim i As Long
Dim FileHandle%
Dim strFileName As String
strFileName = "C:\Testtext.txt"
FileHandle% = FreeFile
i = 0
Open strFileName For Input As #FileHandle%
Do While Not EOF(FileHandle%)
Line Input #FileHandle%, strArray(i)
i = i + 1
Loop
Text1.Text = strArray(0)
Text2.Text = strArray(1)
Text3.Text = strArray(2)
Text4.Text = strArray(3)
Text5.Text = strArray(4)
Text6.Text = strArray(5)
Close FileHandle%
End Sub
My question is...how can I do the same thing, but read each line from a
multiline textbox instead of a text file?
Thanks in advance,
Ron
-
Re: How to read each line from multiline textbox?
The easy way is to do this:
Assuming textbox is txtText, the array this would be stored in is txtarray()
VB Code:
txtarray = split(txtText,VbCrLF)
Yup, it's that simple. split will split the textbox at every VbCrLf (which is a carriage return, at the end of every line) and make an array of the variable you give it (it has to be a unique variable, not one already used)
-
Re: How to read each line from multiline textbox?
I get an error "Can't assign to array"
-
Re: How to read each line from multiline textbox?
-
Re: How to read each line from multiline textbox?
Yeah, sorry...I forgot that bit :-)
-
Re: How to read each line from multiline textbox?
I don't know what I'm doing.....now it says "Expected array"
-
Re: How to read each line from multiline textbox?
I'm not looping through the textbox correctly.........any help?
-
Re: How to read each line from multiline textbox?
in your case it might be "strArray = split..." and "dim strArray as string"...paste new code here if not, and someone will help :-)
-
Re: How to read each line from multiline textbox?
Quote:
Originally Posted by rdcody
I'm not looping through the textbox correctly.........any help?
For the record
VB Code:
strFileName = "C:\Testtext.txt"
FileHandle% = FreeFile
i = 0
Open strFileName For Input As #FileHandle%
Do While Not EOF(FileHandle%)
Line Input #FileHandle%, strArray(i)
i = i + 1
Loop
Is replaced totally by my split line :-)
-
Re: How to read each line from multiline textbox?
Are saying this is all I need...
VB Code:
Private Sub Command1_Click()
Dim strArray As String
strArray = Split(txtText, vbCrLf)
Text1.Text = strArray(0)
Text2.Text = strArray(1)
Text3.Text = strArray(2)
End Sub
-
Re: How to read each line from multiline textbox?
VB Code:
Dim txtarray() As String
txtarray = Split(txtText, vbNewLine)
:)
-
Re: How to read each line from multiline textbox?
I get the first error "Can't assign to array"
-
Re: How to read each line from multiline textbox?
Knew I could see a problem with that dim line...no () after the string...sorry. And for the record, both vbNewLine and VbCrLf are the same thing so either is valid :-)
Cody, re-paste the (updated) code snippet
-
Re: How to read each line from multiline textbox?
Don't I need to loop thru something :-)
-
Re: How to read each line from multiline textbox?
As I said above, there's NO LOOP with a split. Re-paste your code if you want people to be able to easily assist you
-
Re: How to read each line from multiline textbox?
ok smUX ..........
VB Code:
Private Sub Command1_Click()
Dim strArray As String
strArray = Split(txtText, vbCrLf)
Text1.Text = strArray(0)
Text2.Text = strArray(1)
Text3.Text = strArray(2)
End Sub
-
Re: How to read each line from multiline textbox?
strArray is an array (not a variable) so declare it with ()
(RobDog888 had already pointed that out)
-
Re: How to read each line from multiline textbox?
some people are saying to do it this way....
others, this way...
neither one seems to work
-
Re: How to read each line from multiline textbox?
You could instead make the textboxes an array and use less code
VB Code:
Private Sub Command1_Click()
Dim strArray() As String
strArray = Split(txtText, vbCrLf)
for cur = 0 to 2
Text1(cur).Text = strArray(cur)
next cur
End Sub
Just delete text2 and text3 then copy/paste text1...when it asks if you want to make an array say yes and paste again...you'll have 3 textboxes indexed 0 to 2 :-)
-
Re: How to read each line from multiline textbox?
My code works as does others. Can you state the error message your getting and with what code?
VB Code:
'Works and declares a string array
Dim strArray() As String
-
Re: How to read each line from multiline textbox?
I'll see if that works in a second, but I don't really want to create an array
out of the textboxes because it would be confusing with the rest of the
code. You know what I mean....now what was Text1(4)?
-
Re: How to read each line from multiline textbox?
I have started a new project, put this in the source:
VB Code:
Private Sub Command1_Click()
Dim strArray() As String
strArray = Split(txtText, vbCrLf)
For cur = 0 To 2
Text1(cur).Text = strArray(cur)
Next cur
End Sub
Private Sub Form_Load()
txtText = "a" & vbCrLf & "b" & vbCrLf & "c"
End Sub
and added a textbox called "txtText" and the 3 arrayed text1(0) to text1(2) plus the command1 button and it works fine. Be aware that if your input string doesn't have 2 vbcrlf in you are GOING to have errors as there isn't 3 strings to put in...in this case, change "Text1(cur).Text = strArray(cur)" to "if cur <= ubound(strArray()) then Text1(cur).Text = strArray(cur)"
-
Re: How to read each line from multiline textbox?
You dont need to make the textboxes an array, just your string variable that receives the text file contents.
-
Re: How to read each line from multiline textbox?
Hey RedDog888
VB Code:
Private Sub Command1_Click()
Dim strArray() As String
strArray = Split(txtText, vbCrLf)
Text1.Text = strArray(0)
Text2.Text = strArray(1)
Text3.Text = strArray(2)
End Sub
I get the error "Can't assign to array"
-
Re: How to read each line from multiline textbox?
Quote:
Originally Posted by rdcody
I get the error "Can't assign to array"
after the split line put "debug.print ubound(strArray())" and tell us what it puts in your "immediate" window
-
Re: How to read each line from multiline textbox?
Thats because your variable "txtText" or is it a textbox is not populated so its a nullstring value.
-
Re: How to read each line from multiline textbox?
reddog888 :-) .........sorry about that RobDog888
-
Re: How to read each line from multiline textbox?
you can't populate it in design mode?
line 1
line 2
line 3
-
Re: How to read each line from multiline textbox?
Yes, but is it a textbox then?
-
Re: How to read each line from multiline textbox?
i don't understand.....
it's a textbox with multiline set to true and this text...
line 1
line 2
line 3
-
1 Attachment(s)
Re: How to read each line from multiline textbox?
Here is a very basic example using the code and a prepopulated multiline textbox.
-
Re: How to read each line from multiline textbox?
well, that's the same thing I just posted, and I get the same error
-
Re: How to read each line from multiline textbox?
Did you run my project? It runs fine and populates textbox 1 - 3 with the multiline textbox contents.
-
Re: How to read each line from multiline textbox?
yes.....same error
I'm using VB5, and I have Split function in a module....maybe that's
why it won't work
-
Re: How to read each line from multiline textbox?
when you say module, do you mean a module WITHIN the form or outside of it...if outside, you need to reference the form in the name as in form1.txtText.text =...
-
Re: How to read each line from multiline textbox?
Module1 in Project1 with Form1
VB Code:
Public Function Split(ByVal sIn As String, _
Optional sDelim As String = " ", _
Optional nLimit As Long = -1, _
Optional bCompare As VbCompareMethod = vbBinaryCompare) _
As Variant
Dim nC As Long, nPos As Long, nDelimLen As Long
Dim sOut() As String
If sDelim <> "" Then
nDelimLen = Len(sDelim)
nPos = InStr(1, sIn, sDelim, bCompare)
Do While nPos
ReDim Preserve sOut(nC)
sOut(nC) = Left(sIn, nPos - 1)
sIn = Mid(sIn, nPos + nDelimLen)
nC = nC + 1
If nLimit <> -1 And nC >= nLimit Then Exit Do
nPos = InStr(1, sIn, sDelim, bCompare)
Loop
End If
ReDim Preserve sOut(nC)
sOut(nC) = sIn
Split = sOut
End Function
-
Re: How to read each line from multiline textbox?
Yes thats the problem. If you have the code in a module then you need to fully qualify the textboxes with the form name like Form1.Text1.Text etc.
So you didnt run my project then or is that VB 5 doesnt support the Split function? Split is a built in function in VB6 so not sure about VB 5.
-
Re: How to read each line from multiline textbox?
Looks to me like split() isn't supported if he had to use that bit of code above :-)
-
Re: How to read each line from multiline textbox?
Just making sure as the function was not posted yet. Perhaps we need to see the entire project as things are not exactly as stated.
-
Re: How to read each line from multiline textbox?
geeeeeezzzzz guys...
the textbox code is in Form1
the split function is in Module1
vb5 does not support the split function that's built into vb6
that's why i'm using the split function i posted.