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