Private Sub Form_Click()
Dim Arr() As String
Dim S As String
S = Text1.Text
Arr = Split(S, vbCrLf)
MsgBox Mid(Arr(4), 5, 1)
End Sub
但不知道:
如何获得一个richtextbox中总的行数呢?
...全文
5964打赏收藏
如何获得一个richtextbox中总的行数呢?
我使用以下代码来获得特定行的特定字符 Private Sub Form_Click() Dim Arr() As String Dim S As String S = Text1.Text Arr = Split(S, vbCrLf) MsgBox Mid(Arr(4), 5, 1) End Sub 但不知道: 如何获得一个richtextbox中总的行数呢?
'Adding line numbers to the front of text in a rich text box
'Example by HeSaidJoe
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 Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
Private Const EM_GETLINE = &HC4
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1
Private Sub Command1_Click()
Dim lngCount As Long
Dim lngLineIndex As Long
Dim lngLength As Long
Dim strBuffer As String
Dim strRichText As String
Dim i As Integer
'Get Line count
lngCount = SendMessage(RichTextBox1.hwnd, EM_GETLINECOUNT, 0, 0)
With RichTextBox1
For i = 0 To lngCount - 1
'Get line index
lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, i, 0)
'get line length
lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0)
'resize buffer
strBuffer = Space(lngLength)
'get line text
Call SendMessageStr(.hwnd, EM_GETLINE, i, ByVal strBuffer)
'Number each line
strRichText = strRichText & CStr(i + 1) & " " & strBuffer & vbCrLf
Next
'rewrite numbered text in RichTextBox
.Text = strRichText
End With
End Sub
Private Sub Form_Load()
With RichTextBox1
.Text = "line one" & vbCrLf
.Text = .Text & "line two" & vbCrLf
.Text = .Text & "line three"
End With
End Sub