在VB中如何获取TextBox的行数?
发布网友
发布时间:2022-04-30 21:57
我来回答
共4个回答
热心网友
时间:2022-06-19 05:26
'获得光标位置(按字节计算,起如行和起始列均为0)
Private Function GetCurPos(ptPos As POINTAPI) As Long
Dim nLine As Long, nCol As Long
Call SendMessage(Me.Text1.hwnd, EM_GETSEL, 0, ptPos)
nLine = SendMessage(Me.Text1.hwnd, EM_LINEFROMCHAR, ptPos.x, ByVal 0&) '获得行号
nCol = ptPos.x - SendMessage(Me.Text1.hwnd, EM_LINEINDEX, -1, 0) '获得列号
GetCurPos = ptPos.x '光标位置(按字节计算)
ptPos.y = nLine
ptPos.x = nCol
End Function
'获得光标位置(按字符计算)
Private Function GetCurPosCh(ptPos As POINTAPI) As Long
Dim nLine As Long, nCol As Long
Dim ByteBuffer() As Byte
SendMessage Me.Text1.hwnd, EM_GETSEL, 0, ptPos
nLine = SendMessage(Me.Text1.hwnd, EM_LINEFROMCHAR, ptPos.x, ByVal 0&) '获得行号
nCol = ptPos.x - SendMessage(Me.Text1.hwnd, EM_LINEINDEX, -1, 0)
ReDim ByteBuffer(1024)
ByteBuffer(1) = 4
SendMessage Me.Text1.hwnd, EM_GETLINE, nLine, ByteBuffer(0)
ReDim Preserve ByteBuffer(nCol)
nCol = Len(StrConv(ByteBuffer, vbUnicode)) - 1 '获得列号
GetCurPosCh = Me.Text1.SelStart '光标位置(按字符计算)
ptPos.y = nLine + 1
ptPos.x = nCol + 1
Erase ByteBuffer
End Function
Call GetCurPosCh(ptPos)
GetTextInfo = " 第 " & ptPos.y & " 行 , 第 " & ptPos.x & " 列"
热心网友
时间:2022-06-19 05:26
首先,题主要明确这并不是你的程序出了什么问题,题主贴出的代码是检测 Text1.Text 中包含多少个 vbCrlf(即回车符)的,倘若 Text1.Text 某一行字符过长,在末尾处“折叠”到了下一行,其实准确的来说,在系统看来这仍然是一行字,因为系统识别两行字就是判断有无回车符或换行符(在C语言里面就是 \n),所谓的“多行”是题主肉眼识别出来的。
可以考虑用计算字符宽度和 Text1 的宽度来计算“每行”能容纳多少个字符,然后总字符数除以单位容纳字符数就时所谓的"行数"了。
热心网友
时间:2022-06-19 05:27
Const EM_GETLINECOUNT = 186
Private Declare Function SendMessageAsLong Lib "user32" _
Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Private Sub Command1_Click()
Dim lCount As Long
lCount = SendMessageAsLong(Text1.hWnd, EM_GETLINECOUNT, 0, 0)
MsgBox lCount
End Sub
热心网友
时间:2022-06-19 05:27
自动换行的情况并不会生成vbcrlf符号。
只有按了回车键才会有vbcrlf符号。所以你用split分割vbcrlf当然只能得到1的结果。
对于自动换行的内容,不能算是多行,只算一行。如果非要计算行数,要结合字符宽度和文本框的宽度来计算了。