Get a string of a line in a textbox

Source: Internet
Author: User

This is done using the Em_getline message, which is peculiar to the point where the lparam is pointing to a string, but when the string is passed in, the first two byte will have the maximum length allowed for the string.

'以下在Form需一个TextBox,并设定MultiLine = True, 一个Command Button
Private Sub Command1_Click()
Dim str5 As String
str5 = GetaLine(Text1,1) '取得第二行的字串,以0为基底
End Sub
'以下在.Bas
Option Explicit
Const EM_GETLINE = &HC4
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
     (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
     lParam As Any) As Long
Public Function GetaLine(Text1 As TextBox, ByVal ntx As Long) As String
Dim str5(255) As Byte '如果您的字串 > 255 byte请自行增加该Byte Array
Dim str6 As String, i As Long
str5(0) = 255 '字串的前两个Byte存该字串的最大长度
str5(0) = 255
i = SendMessage(Text1.hwnd, EM_GETLINE, ntx, str5(0))
If i = 0 Then
   GetaLine = ""
Else
   str6 = StrConv(str5, vbUnicode)
   GetaLine = Left(str6, InStr(1, str6, Chr(0)) - 1)
End If
End Function

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.