Solved

RichTextBox Forecolor

Posted on 2004-04-10
12
1,256 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
12 Comments
 
LVL 15

Accepted Solution

by:
ameba earned 125 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
 
LVL 85

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now