?
Solved

RichTextBox Forecolor

Posted on 2004-04-10
12
Medium Priority
?
1,306 Views
Last Modified: 2013-11-13
Normal text box has the forecolor option no matter if you set a new text in it during run time or design time the text is still the color you specified.

I want to use a RichTextBox because I will be entering more than 65535 characters into the text area but my problem is I need the text I set during the runtime white.

Yea RichTextBox provides the SelColor option but that's only if you enter the text manually it's colored, the box will be locked and all the text will the automatically set.  No matter what I try I can't get the new set text to be any other color than black.  Can someone help?
0
Comment
Question by:NotAvailable
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 15

Accepted Solution

by:
ameba earned 375 total points
ID: 10796064
Two ways to use SelColor:

' Form1, add richtextbox
Option Explicit

Private Sub Form_Click()
    RichTextBox1.SelStart = 0
    RichTextBox1.SelLength = Len(RichTextBox1.Text)
    RichTextBox1.SelColor = vbBlue
End Sub

Private Sub Form_Load()
    RichTextBox1.SelColor = vbRed
    RichTextBox1.SelText = "This is red."
End Sub
0
 
LVL 1

Author Comment

by:NotAvailable
ID: 10796137
Yes it does the job but it's inefficient with more than 120 000 characters in the control.  Lags it a little with slower computers.
0
 
LVL 15

Expert Comment

by:ameba
ID: 10796165
There is faster method, but it is very complicated - defining table of colors and modifying rtf code.

SelColor + SelText is fast enough in most cases. What is the time for 120000 chars? Maybe you can post your code.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 10796645
This was the fastest method I could come up with.  Sadly, it took approximately 28 seconds on my system.  I tried using the LockWindowUpdate() API instead of toggling the Visibility, but that actually made it slower.

In my appendPlainText() sub, this code snippet made the most gains:
    Static charsInBox As Long
    charsInBox = charsInBox + Len(plainText)
    RichTextBox1.SelStart = charsInBox
as opposed to this method:
    RichTextBox1.SelStart = Len(RichTextBox1.Text)

Regards,

Idle_Mind

Option Explicit

Private Sub Form_Load()
    RichTextBox1.BackColor = vbBlack
    RichTextBox1.SelStart = 0
    RichTextBox1.SelLength = Len(RichTextBox1.Text)
    RichTextBox1.SelColor = vbWhite
End Sub

Private Sub Command1_Click()
    Dim i As Long
    Dim start As Single
   
    start = Timer
    RichTextBox1.Visible = False
    For i = 1 To 120000
        appendPlainText "/"
    Next i
    RichTextBox1.Visible = True
    Label1.Caption = Timer - start
    MsgBox "Done"
End Sub

Private Sub appendPlainText(ByRef plainText As String)
    Static charsInBox As Long
    charsInBox = charsInBox + Len(plainText)
    RichTextBox1.SelStart = charsInBox
    RichTextBox1.SelLength = 0
    RichTextBox1.SelText = plainText
End Sub
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 10799392
Takes 2 seconds for me (1.5GHz) to change the color of 600,000 characters.. am I missing the problem?

Private Sub Form_Load()
    RichTextBox1.Text = String(600000, "x")
    RichTextBox1.SelStart = 0
    RichTextBox1.SelLength = Len(RichTextBox1.Text)
    RichTextBox1.SelColor = vbWhite
End Sub
0
 
LVL 15

Expert Comment

by:ameba
ID: 10851206
So, you are punishing me with a C, because you don't want to discuss your second question about performance.
0
 
LVL 15

Expert Comment

by:ameba
ID: 10902418
Why B?
0
 
LVL 15

Expert Comment

by:ameba
ID: 10903545
I see, so, after questioner insults me with an unjustified bad grade, it is EE's turn to do the same.
0
 

Expert Comment

by:jstirrup
ID: 11555860
I found a friendlier solution using the same concept. I am writing a program that keeps a rolling status of a winsock session. I want the RichTextBox to look traditional 'DOS'. Here is what I did:

Sub WriteToStatusBox(strMessage as Stirng)

    'This resets the cursor to the end (in case you don't have the RichText Box Locked)
    rtbStatus.SelStart = Len(rtbStatus.Text) + 1

    'Add your text    
    rtbStatus.Text = rtbStatus.Text & strMessage & vbcrlf
   
    'This selects only the new text
    rtbStatus.SelLength = Len(strMessage)
   
    rtbStatus.SelColor = vbWhite

    'This resets the start to the last char for cursor placement)
    rtbStatus.SelStart = Len(rtbStatus.Text)  + 1

End Sub

Hope you still need help on this. I know it may be late, but it works. Good Luck
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question