When the text box property is allowed to allow multiple lines, since it is a branch implemented by a soft return, the contents of the specified row cannot be accurately removed with Split (Text1.Text, VBCRLF). This article uses the SendMessage series function, implements a line containing the specified string and demonstrates how to obtain text boxes in text boxes and arbitrary designated lines of text boxes and arbitrary specified lines in text boxes.
'Add a TextBox with "multiline = true", "scrollbars = 2".
Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" _ (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ lParam As Any) As LongPrivate Declare Function SendMessageByNum Lib "USER32" _ Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, ByVal lParam As Long) As LongPrivate Declare Function SendMessageByString Lib "USER32" Alias _ "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam _ As Long , BYVAL LPARAM AS STRING AS Long
Private const EM_LINEINDEX = & hbbprivate const EM_GETLINECUNT = & HBAPRIVATE CONST EM_GETLINE = & HC4Private const em_lineLength = & HC1
Function GetLineText (ByVal txtbox As TextBox, ByVal LineIndex As Long) As String 'returns the contents of the specified row Dim lc As Long, linechar As Long linechar = SendMessageByNum (txtbox.hWnd, EM_LINEINDEX, LineIndex, 0) lc = SendMessageByNum (txtbox.hWnd , EM_LINELENGTH, LINECHAR, 0) 1 getLinetext = String (LC 2, 0) MID (GetLinetext, 1, 1) = CHR (LC AND HFF) MID (GetLinetext, 2, 1) = CHR (LC / & H100) LC = SendMessageBystring (txtbox.hwnd, em_getline, lineindex, getLinetext) getLinetext = left (getLinetext, lc) end function
Function getlinewithstr (ByVal txtbox As TextBox, ByVal mystr As String) As StringDim linecount As Long, temp () As String, i As Longlinecount = SendMessage (txtbox.hWnd, EM_GETLINECOUNT, 0, 0) 'number of rows returned ReDim temp (1 To Linecount) for i = 1 to LineCountTemp (i) = "" & I & "line:" & getLinetext (txtbox, i - 1) 'add line number NextGetLineWithstr = Join (FILTER (TEMP, MYSTR), VBCRLF)' characters Out-filtered Erase Tempend FunctionPrivate Sub Command1_Click () MSGBox getLinewithstr (Text1, "9CBS"), 0, "9CBS" end Sub
PRIVATE SUB FORM_LOAD () DIM A (25) AS String, I as longfor i = 0 to 25A (i) = String (50, CHR (i 97)) NextText1.text = JOIN (A, "9CBS")
End Sub