根据补充信息,修改如下:
楼上几位的方法都会使Richtextbox格式发生变化。
我的做法是通过API来实现:
一、把下面代码放入模块中:
Option Explicit
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 SendMessageByNum Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private 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_LINELENGTH = &HC1
Private Const EM_REPLACESEL = &HC2
Private Const EM_SETSEL = &HB1
Private Const EM_GETSEL = &HB0
Public Sub AddText(RichTextBoxName As Object, SendString As String, Optional Position As String)
Dim K As Long, Hwd As Long
Hwd = RichTextBoxName.hWnd
Select Case LCase(Position)
Case "top"
K = 0
Case "end"
K = Len(RichTextBoxName.Text)
Case Else
SendMessage Hwd, EM_GETSEL, 0, K
End Select
SendMessageByNum Hwd, EM_SETSEL, K, K
SendMessageByString Hwd, EM_REPLACESEL, 1, SendString
End Sub
二、调用举例:
AddText RichTextBox1, "11111", "top" '在文本头插入
AddText RichTextBox1, "2222", "end" '在文本尾追加
AddText RichTextBox1, "3333" '在当前位置插入
通过上述方法追加的文本不会影响原有的格式。
温馨提示:答案为网友推荐,仅供参考