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
Solved

RichTextBox Forecolor

Posted on 2004-04-10
12
1,286 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
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
 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
WPF - Tooltips for ComboBox items 5 95
Child Form in front 4 49
vbModal 12 57
adding "ungroup sheets" to existing vbs code 5 31
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

839 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