更改vb6中富文本框的颜色 [英] change the color of rich text box in vb6
问题描述
亲爱的先生
i使用此代码更改富文本框的颜色
但是当我输入新行时它改变了以上lines.i的颜色只是想改变当前行的颜色。
第一行的第一次字体颜色是红色
第二次第二行的字体颜色是绿色
但第三次第二行的字体颜色从绿色变为红色
请帮帮我
Dim start1 作为 整数
start1 = Len(RichTextBox1.Text)
如果 RichTextBox1.Text<> 然后
RichTextBox1 .Text = RichTextBox1.Text& vbCrLf& Text1.Text
使用 RichTextBox1
.SelStart = start1
.SelLength = Len(RichTextBox1)
.SelColor = vbGreen
结束 使用
Else
RichTextBox1.Text = Text1.Text
使用 RichTextBox1
.SelStart = start1
.SelLength = Len(RichTextBox1)
.SelColor = 255
结束 使用
结束 如果
结束 Sub
谢谢先生
您的主要问题是您向RTB添加文本的方式RichTextBox1.Text = RichTextBox1.Text& vbCrLf& Text1.Text
文本是,嗯......只是文本。它没有任何颜色,格式等概念。例如将NotePad(文本编辑器)与MS Word(富文本编辑器)之类的东西进行比较
下一个问题是你选择的方式.SelLength
.SelLength = Len(RichTextBox1)首先 - 你绝对是确定该控件的默认属性是
Text
属性?。SelLength = Len(RichTextBox1 .Text )本来会更好,但仍然无法修复您的问题,因为您选择了整个RTB的文本。
您最好使用RTB的.SelText
属性...设置选择的起点,设置所需的颜色,然后将所选文本设置为要插入的信息。像这样
Dim start1 As < span class =code-keyword>整数
Dim lCurrColour As < span class =code-keyword>长
start1 = Len(RichTextBox1.Text)
如果 RichTextBox1 .Text<> 然后
使用 RichTextBox1
.SelStart = start1
lCurrColour = .SelColor
.SelColor = vbGreen
.SelText = vbCrLf& Text1.Text
.SelColor = lCurrColour
结束 使用
< span class =code-keyword> Else
... etc请注意,我将在我要插入行的位置捕获当前颜色 -
lCurrColour
然后在插入后将其设置回来。
如果这是我,我会重构代码以避免重复代码/工作。我个人偏好if
声明尽可能接近受其影响的事物(NB - 个人偏好 - 这只是我,其他人可能不同意)。我会做更像这样的事情Dim lCurrColour As 长
使用 RichTextBox1
.SelColor = IIf(RichTextBox1.Text = < span class =code-string> , 255 ,vbGreen)
lCurrColour = .SelColor
.SelStart = Len(RichTextBox1.Text)
.SelText = Text1.Text& vbCrLf
.SelColor = lCurrColour
结束 使用
Dear Sir
i am changing the color of rich text box using this code
but when i enter the new line it change the color of above lines.i just want to change the color of current row.
first time font color of row one is red
second time the font color of row two is green
but third time the font color of second row change from green to again red
please help me out
Dim start1 As Integer
start1 = Len(RichTextBox1.Text)
If RichTextBox1.Text <> "" Then
RichTextBox1.Text = RichTextBox1.Text & vbCrLf & Text1.Text
With RichTextBox1
.SelStart = start1
.SelLength = Len(RichTextBox1)
.SelColor = vbGreen
End With
Else
RichTextBox1.Text = Text1.Text
With RichTextBox1
.SelStart = start1
.SelLength = Len(RichTextBox1)
.SelColor = 255
End With
End If
End Sub
thanks sir
Your main problem is the way you are adding text to your RTBRichTextBox1.Text = RichTextBox1.Text & vbCrLf & Text1.Text
Text is, well ... just text. It doesn't have any concept of colour, format etc. For example compare NotePad (a text editor) to something like MS Word (a rich text editor)
The next problem is the way you are selecting.SelLength
.SelLength = Len(RichTextBox1)Firstly - Are you absolutely sure sure that the default property of this control is its
Text
property?.SelLength = Len(RichTextBox1.Text)would have been better, but still wouldn't fix your problem as you are selecting the entire text of the RTB.
You will be better off using the.SelText
property of the RTB ... set the start point of the selection, set the colour you want, then set the selected text to be the information you want to insert. Like this
Dim start1 As Integer Dim lCurrColour As Long start1 = Len(RichTextBox1.Text) If RichTextBox1.Text <> "" Then With RichTextBox1 .SelStart = start1 lCurrColour = .SelColor .SelColor = vbGreen .SelText = vbCrLf & Text1.Text .SelColor = lCurrColour End With Else ...etcNotice that I capture the current colour at the point I'm going to insert the line -
lCurrColour
and then set it back after the insert.
If this was me I would refactor the code to avoid duplication of code/effort. I have a personal preference for anif
statement being as close as possible to the thing that is affected by it (NB - personal preference - that's just me, others might disagree). I would have done something more like thisDim lCurrColour As Long With RichTextBox1 .SelColor = IIf(RichTextBox1.Text = "", 255, vbGreen) lCurrColour = .SelColor .SelStart = Len(RichTextBox1.Text) .SelText = Text1.Text & vbCrLf .SelColor = lCurrColour End With
这篇关于更改vb6中富文本框的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!