how to read data from multiline textbox
suppose i have text1.text (Multiline Property set true)
Code:line1
line2
line3
line4
I want to read line by line text from multiline textbox.........
Printable View
how to read data from multiline textbox
suppose i have text1.text (Multiline Property set true)
Code:line1
line2
line3
line4
I want to read line by line text from multiline textbox.........
There are a few methods.
1. You can use VB's Split() function
2. You can use APIs to read each line, however, each line is what you physically see on the screen, taking into consideration wordwrapping if applicable. Instead of reposting what I wrote earlier today, I'll just direct you to this postCode:Dim sLines() As String, L As Long
sLines = Split(Text1.Text, vbCrLf)
For L = 0 To UBound(sLines)
' read each sLines(L)
Next
What do you mean by "read"? Once you have each line, what do you want to do with it?
Question for me, Hack? If so, poor choice of words on my part. I should have added a comment like, "Process each sLines(L)". Oops.Quote:
Originally Posted by Hack
Edit:Code:'Read line in multiline textbox
Option Explicit
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 Function SendMessageStr Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINELENGTH = &HC1
Private Const EM_LINEINDEX = &HBB
Private Const EM_GETLINE = &HC4
Private Sub Command1_Click()
Dim TextCount As Long, TextLineIndx As Long, TextLineLen As Long, i As Long
Dim strBuffer As String
'-- Get the line count of Textbox
TextCount = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, 0)
With Text1
For i = 0 To TextCount - 1
'-- Get Line Index of the chosen line
TextLineIndx = SendMessage(.hwnd, EM_LINEINDEX, i, 0)
'-- Get line length in Textbox
TextLineLen = SendMessage(.hwnd, EM_LINELENGTH, TextLineIndx, 0)
'-- Resize buffer
strBuffer = Space(TextLineLen)
'-- Get line text
Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer)
'-- Display Line
MsgBox strBuffer
Next
End With
End Sub
@Catchit: If you would have searched the forums, you would have see that martinliss had answered a similar kind of query... That is why it is good to search the forums before you post ;)
koolsid, that code may work for TextBox controls but not for RichTextBox controls
I know the question is for textbox controls, but I think when this method is posted it should come with a caveat: Do not use for the RichTextBox control, crashes can occur.
FYI: Reason why it doesn't work. The buffer used for EM_GETLINE is not prepared correctly. The link I referred to in para 2 of post #2 will fix that problem.
I disagree LV. I have been using the same techqnique successfully for RTB without any trouble. See Code Generator link in my signature :)
koolsid, it isn't really a matter to debate, no offense intended. Per MSDN documentation, the EM_GETLINE's buffer's first 4 bytes must contain the size of the string. Since the string is filled with spaces, the reported buffer size would be &H20202020 for ANSI calls and &H20002000 (I think) for unicode.
I have seen similar code posted numerous times and have corrected it where I saw it. The method used will show its error/crash potential on short strings (1-3 chars) most of the time and will not work with Unicode strings either.
IMHO, just the fact that it works at all is simple luck. We know that using improper parameters with other SendMessage messages may cause crashes and/or garbled data sometmes and maybe everytime. This is just another example. The OP of the link I referenced in post #2 was crashing. Granted it might be due to other issues, but as soon as he tweaked the code to prepare the buffer correctly, the crashes stopped immediately.
Here is the result of the code you posted. The only thing I did was add a RTB control and called it Text1. You can see the 4 lines of text I added to the RTB on form load
Attachment 69114
P.S. Why does it work on textbox controls and not richtextbox controls? My guess is that the default window procedure for textbox controls does not use the buffer for "pre-processing" but maybe the window procedure for rtbs do? But that is just a guess. I prefer to prepare the buffer per MSDN specs vs taking my chances.
none taken :)Quote:
no offense intended
I will check out the info you just gave. if it is true then this is interesting...
Edit: Wait The code that I posted above is for textbox, for RTB, you need to use the code which I have mentioned in my previous link. I am talking about the code in
http://www.vbforums.com/showpost.php...8&postcount=10
The example in the link you just added works well. The reason is that your buffer array's 1st 4 bytes contains the length of the buffer
The EM_GETLINE can be used for RTBs with typical VB strings, without using an array and without StrConv calls if the 1st 4 bytes of the VB string are modified before it is sent. Therefore, one does not need 2 different methods of obtaining the same results for textbox & RTB controls
Also, just as a curiosity. I am not exactly sure why your code works for 1 or 2 character strings. Your buffer is less than 4 bytes in that case and if the control is looking for the length in the 1st 4 bytes, and actually tries to read 4 bytes from a 2-3 byte array, why problems are not experienced? Guesses is all that I have.
The caveat i mentioned does apply and I think you would agree now?
Edited: Oops, a bug in your logic. If a line of text is > 254 chars, you will get an overflow in this line: Buffer(0) = RowLength+1
The link was all the time there. If you hover your mouse over the word "disagree" in post 7 you'll see the link :)Quote:
The example in the link you just added works well.
I haven't tested that... let me check that for you... but if you have a project ready, you can upload it or mail it me (pm ing you my email address).Quote:
Oops, a bug in your logic. If a line of text is > 254 chars, you will get an overflow in this line: Buffer(0) = RowLength+1
Man, I sure love these discussions. Great going LV
koolsid, I have been reading your RTB post & will pm you about some questions.
To replicate the long string length, set the multiline to false and add a test string of String$(300,"a") for example.
Thanks........
Everyone
Yeah you are right, it gives an overflow error. I see what you mean but also I was thinking who would have an RTB with more that 50 chars in the first line, leave alone 255... just a thought...
Quote:
Originally Posted by CatchItBaby
if i want to add some text in line 2 then how should i go.......
means
Code:line1
i want to put some text here line2
line3
line4