VB quickly reads data of row n of textbox

Source: Internet
Author: User

Textbox uses vbcr + vblf as the branch symbol. If we want to read each line of textbox one by one, it is nothing more than looking for the position of vbcr + vblf, and then extracting the strings of each line, but this method is not fast, and if we want to read the nth row of data, we still need to read from 1st, 2 ,... N-1 reading one by one, it is really troublesome. Fortunately, the Windows API provides the function to read the nth line of textbox. The details are as follows:

1. API declaration:

Public const em_getline = & hc4
Public const em_linelength = & HC1
Public const em_lineindex = & HBB

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)

2. program example:
Sub tb_getline (byval hwnd as long, byval whichline as long, line as string)

Dim length as long, Barr () as byte, barr2 () as byte, LC as long

Lc = sendmessage (hwnd, em_lineindex, whichline, byval 0 &)

Length = sendmessage (hwnd, em_linelength, LC, byval 0 &)

If length> 0 then
Redim Barr (Length + 1) as byte, barr2 (length-1) as byte
Call rtlmovememory (Barr (0), length, 2) 'prepare a memory. Before passing a message, fill in the first two bytes of the memory with the memory length.
Call sendmessage (hwnd, em_getline, whichline, Barr (0 ))
Call rtlmovememory (barr2 (0), Barr (0), length)
Line = strconv (barr2, vbunicode)
Else
Line = ""
End if
'Tutorial China. million tutorials
End sub
'Assume that you want to read the data of 5th rows of text1

Dim s as string

Call tb_getline (text1.hwnd, 5, S)

'Return value S is equal to 5th rows of data

(Note: the number of rows in textbox starts from 0 .)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.