关于RichTextBox控件

xayzmb 2005-01-19 09:07:15
如何使每一行的字体,颜色都不一样
我下面的代码只能修改当前行的颜色
前面的都自动变成黑色了
怎么做???

Private Sub cmdSend_Click()
Static i As Long
'向控件中写入txtSend.text中的内容
If Len(Trim(rxtSend.Text)) = 0 Then
rxtSend.Text = i & ": " & txtSend.Text
Else
rxtSend.Text = rxtSend.Text & Chr(10) & i & ": " & txtSend.Text
End If
'改变写入行颜色
rxtSend.SelStart = Len(rxtSend.Text) - Len(txtSend.Text)
rxtSend.SelLength = Len(txtSend.Text)
If i Mod 2 = 0 Then
rxtSend.SelColor = RGB(240, 0, 0)
Else
rxtSend.SelColor = RGB(0, 0, 240)
End If
i = i + 1
'将插入点移动最后一行
rxtSend.SelStart = Len(Trim(rxtSend.Text))
End Sub

怎么能让它保留修改后的颜色呢?
...全文
95 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
daviddivad 2005-01-20
  • 打赏
  • 举报
回复
Private Sub cmdSend_Click()
Static i As Long
'改变写入行颜色
rxtSend.SelStart = Len(rxtSend.Text)
If i Mod 2 = 0 Then
rxtSend.SelColor = RGB(240, 0, 0)
Else
rxtSend.SelColor = RGB(0, 0, 240)
End If
'向控件中写入txtSend.text中的内容
rxtSend.SelText = vbcrlf & i & ":" & txtSend.Text
i = i + 1
'将插入点移动最后一行
rxtSend.SelStart = Len(Trim(rxtSend.Text))
End Sub
xayzmb 2005-01-19
  • 打赏
  • 举报
回复
TO:楼上
这样写的话
每向里面写一行就得把前面写的重新改变设置一次颜色吗?
这样实在有点不划算呀
有没有在写入时设置这一行的颜色
当写入后面的行时
已被设置过的颜色不改回黑色的方法?
zxsoft 2005-01-19
  • 打赏
  • 举报
回复
忘了注释了:

l = l + Len(s(i)) + 2

这一行后面的2,指的是vbCr 和vbLf两个字符。由于被Split切掉了。所以要加上2个位置。
zxsoft 2005-01-19
  • 打赏
  • 举报
回复
楼上的代码是变一次颜色写一次文字,这与楼主的问题不符。通过代码可以看到,楼主的目的是让RichTextBox里本来就有的文字颜色变为每行颜色都不同的。因此,我写出如下代码:

窗体控件:

RichTextBox RichTextBox1 属性默认
CommandButton Command1 属性默人

窗体代码:

Private Sub Command1_Click()
Dim s() As String, i As Integer, j As Integer, l As Long
s = Split(RichTextBox1.Text, vbCrLf)
l = 0
For i = 0 To UBound(s)
RichTextBox1.SelStart = l
l = l + Len(s(i)) + 2
RichTextBox1.SelLength = Len(s(i))
RichTextBox1.SelColor = RGB(Int(Rnd * 255), Int(Rnd * 255), Int(Rnd * 255))
Next
End Sub


Private Sub Form_Load()
RichTextBox1.Text = "This is a test Line" & vbCrLf & "This is a test Line" & vbCrLf & "This is a test Line" & vbCrLf & "This is a test Line" & vbCrLf & "This is a test Line" & vbCrLf & "This is a test Line" & vbCrLf & "This is a test Line"
End Sub
daviddivad 2005-01-19
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Const CON_STR As String = "Just a test!"
Dim nColor As Long
Dim nRed As Integer, nBlue As Integer, nGreen As Integer
nRed = Int(Rnd * 255)
nGreen = Int(Rnd * 255)
nBlue = Int(Rnd * 255)
nColor = RGB(nRed, nGreen, nBlue)
With Me.RichTextBox1
.SelStart = Len(.Text)
.SelColor = nColor
.SelText = CON_STR & vbCrLf
End With
End Sub

1,451

社区成员

发帖
与我相关
我的任务
社区描述
VB 控件
社区管理员
  • 控件
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧