Results 1 to 15 of 15

Thread: how to read data from multiline textbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    382

    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.........

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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?

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: how to read data from multiline textbox

    Quote 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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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

    Name:  rtbError.JPG
Views: 6167
Size:  5.9 KB

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: how to read data from multiline textbox

    no offense intended
    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

  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  12. #12
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    382

    Re: how to read data from multiline textbox

    Thanks........

    Everyone

  14. #14
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2008
    Posts
    382

    Re: how to read data from multiline textbox

    Quote 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
  •  



Click Here to Expand Forum to Full Width