更改vb6中富文本框的颜色

编程入门 行业动态 更新时间:2024-10-26 17:28:40
本文介绍了更改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 整数 Dim lCurrColour As 长 start1 = Len(RichTextBox1.Text) 如果 RichTextBox1 .Text<> 然后 使用 RichTextBox1 .SelStart = start1 lCurrColour = .SelColor .SelColor = vbGreen .SelText = vbCrLf& Text1.Text .SelColor = lCurrColour 结束 使用 Else ... etc

请注意,我将在我要插入行的位置捕获当前颜色 - lCurrColour 然后在插入后将其设置回来。 如果这是我,我会重构代码以避免重复代码/工作。我个人偏好 if 声明尽可能接近受其影响的事物(NB - 个人偏好 - 这只是我,其他人可能不同意)。我会做更像这样的事情

Dim lCurrColour As 长 使用 RichTextBox1 .SelColor = IIf(RichTextBox1.Text = , 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 RTB

RichTextBox1.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 ...etc

Notice 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 an if 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 this

Dim 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中富文本框的颜色

本文发布于:2023-06-09 04:10:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/594950.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本框   颜色   中富

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!