Solved

RichTextBox Forecolor

Posted on 2004-04-10
12
1,267 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How To Loop - Python 19 79
using Access 8 58
MS Access Search and Replace Using VBA 6 57
VBA filters 2 50
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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 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

17 Experts available now in Live!

Get 1:1 Help Now