Solved

RichTextBox Forecolor

Posted on 2004-04-10
12
1,302 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 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

696 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