|
-
Feb 14th, 2009, 01:35 PM
#1
Thread Starter
Hyperactive Member
how to read data from multiline textbox
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.........
-
Feb 14th, 2009, 01:45 PM
#2
Re: how to read data from multiline textbox
There are a few methods.
1. You can use VB's Split() function
Code:
Dim sLines() As String, L As Long
sLines = Split(Text1.Text, vbCrLf)
For L = 0 To UBound(sLines)
' read each sLines(L)
Next
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 post
-
Feb 14th, 2009, 05:05 PM
#3
Re: how to read data from multiline textbox
What do you mean by "read"? Once you have each line, what do you want to do with it?
-
Feb 14th, 2009, 05:27 PM
#4
Re: how to read data from multiline textbox
 Originally Posted by Hack
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.
-
Feb 15th, 2009, 02:15 AM
#5
Re: how to read data from multiline textbox
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
Edit:
@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
Last edited by Siddharth Rout; Feb 15th, 2009 at 02:22 AM.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Feb 15th, 2009, 09:46 AM
#6
Re: how to read data from multiline textbox
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.
-
Feb 15th, 2009, 10:09 AM
#7
Re: how to read data from multiline textbox
I disagree LV. I have been using the same techqnique successfully for RTB without any trouble. See Code Generator link in my signature
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Feb 15th, 2009, 10:25 AM
#8
Re: how to read data from multiline textbox
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

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.
Last edited by LaVolpe; Feb 15th, 2009 at 10:52 AM.
-
Feb 15th, 2009, 10:48 AM
#9
Re: how to read data from multiline textbox
none taken 
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
Last edited by Siddharth Rout; Feb 15th, 2009 at 10:56 AM.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Feb 15th, 2009, 11:19 AM
#10
Re: how to read data from multiline textbox
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
Last edited by LaVolpe; Feb 15th, 2009 at 11:33 AM.
-
Feb 15th, 2009, 12:18 PM
#11
Re: how to read data from multiline textbox
The example in the link you just added works well.
The link was all the time there. If you hover your mouse over the word "disagree" in post 7 you'll see the link 
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
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).
Man, I sure love these discussions. Great going LV
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Feb 15th, 2009, 12:20 PM
#12
Re: how to read data from multiline textbox
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.
-
Feb 15th, 2009, 12:32 PM
#13
Thread Starter
Hyperactive Member
Re: how to read data from multiline textbox
-
Feb 15th, 2009, 12:37 PM
#14
Re: how to read data from multiline textbox
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...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Mar 6th, 2009, 12:50 PM
#15
Thread Starter
Hyperactive Member
Re: how to read data from multiline textbox
 Originally Posted by CatchItBaby
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.........
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|