Colored Text In RichTextBox

Ontani

Centurion
Joined
Mar 3, 2004
Messages
121
Location
Belgium
i got a richtextbox were text is entered truw TCP/IP (like messenger)
when the text looks like this:

Ontani says:
Hi
Friend says:
Hey, How your doing
Ontani says:
good, and you...?

it has to color my name (ontani) like in the example

how can i do this?

this is the send code:

Visual Basic:
toSend= userName & " says:" & vbCrLf & txtToSend.Text
txtRecieved.Text &= toSend & vbCrLf
wsTCP.SendData(toSend)
txtToSend.Text = ""

someone an idea?

Greetz
 
You will have to use the SelectionStart and the SelectionLength of the RichTextBox to select your name, and then use SelectedColor to change the color of your name. You will probably have to use .IndexOf to find your name for SelectionStart, or maybe .Find to find it. :)
 
yeah i tried

txtRecieved.SelectionStart = txtRecieved.Find("Ontani")
txtRecieved.SelectionLength = 7
txtRecieved.SelectionColor = Color.MarineBlye

that works for the first time

then it doesn't anymore.

i don't know how to make a loop to select every "Ontani" in the textbox

Greetz
 
Ontani said:
i got a richtextbox were text is entered truw TCP/IP (like messenger)
when the text looks like this:

Ontani says:
Hi
Friend says:
Hey, How your doing
Ontani says:
good, and you...?

it has to color my name (ontani) like in the example

how can i do this?

this is the send code:

Visual Basic:
toSend= userName & " says:" & vbCrLf & txtToSend.Text
txtRecieved.Text &= toSend & vbCrLf
wsTCP.SendData(toSend)
txtToSend.Text = ""

someone an idea?

Greetz


Check this link, looks close to what you are looking for.

http://www.codeproject.com/cs/miscctrl/csexrichtextbox.asp
 
Sorry, I am still trying to wake up. I am not a C# expert, but I know there are a few sites which might be able to turn this into VB code. There is not a lot there, let me see what I can find.

*correction*

Ok, there is a lot more int he source code that you would have to download, and not being a C# person, I am not sure I can convert it anytime soon, sorry about that.
 
Last edited:
Play around with this

Code:
Me.rtbTest.SelectionColor = Color.Blue
Me.rtbTest.SelectedText = "Ontani "
Me.rtbTest.SelectionColor = Color.Black
Me.rtbTest.SelectedText = "Says: Cool"
 
can't get it to work, don't see anything logical in that. :confused:

is it possible to do something like this

set color blue
add "Ontani"
set color black
add " says: "

something like that?

Greetz
 
Try using IndexOf instead of Find. :)

Really all you do is assign TB.Text.IndexOf("Ontani") to the selectionstart instead.
That finds the first Ontani in the textbox.
Everything else that you have after it is fine.

Now, to loop it, you have to use:
X = TB.Text.IndexOf("Ontani", TB.SelectionStart + 1)
'This will find the next Ontani from wherever the selection is.

in a loop of some sort. Preferably a Do Loop that loops until X = -1.
And of course, this check will come at the end.

Now, if you want some pseudocode... this is how you might set up the "IndexOf Loop".
X = indexof ontani
Do Until X = -1
selectionlength
selcolor
X = indexof (next) ontani
Loop
:)
 
isn't there a way to add a color to the text when its added to the richtextbox
cause this is gonna take a huge time when the textbox is full of text

Greetz
 
Sure, but the cursor will have to stay at the end of the textbox.
If you move the cursor to the end of the textbox, you can simply do:

RTB.SelectedText = "Ontani"
RTB.SelectedColor = Color.Red

(or something like that)
:)
 
Iceplug said:
Sure, but the cursor will have to stay at the end of the textbox.
If you move the cursor to the end of the textbox, you can simply do:

RTB.SelectedText = "Ontani"
RTB.SelectedColor = Color.Red

(or something like that)
:)


it was the other way around ;)

RTB.SelectedColor = Color.Red
RTB.SelectedText = "Ontani"

Thanx
 
Damned can't get it done!

I want to add the speaker to the richtextbox without any Style

so just plain text.
and the text to be added next can have a font/size/color.

something like :

Ontani says:
Hey

i used the FontDialog for choosing the font. (what works)
but the adding @ the insertion of the text to the richtextbox something goes wrong.

Visual Basic:
    Private Const WM_VSCROLL As Int32 = &H115
    Private Const SB_BOTTOM As Int32 = 7
    Private Declare Auto Function SendMessage Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32,  _
ByVal lParam As Int32) As Int32
    Dim User as String = "Ontani"

...

        RTB.Text &= User & " says: " & vbCrLf
        SendMessage(RTB.Handle, WM_VSCROLL, SB_BOTTOM, 0)
        RTB.SelectionFont = FontDialog1.Font
        RTB.SelectedText = TextBox1.Text & vbCrLf

the sendmessage takes sure the carret (cursor) goes to the end of the richtextbox. (well it should)

the text in the textbox is added in front of everything.
Can someone help me with this problem

Greetz.
 
I'm just throwing an idea out there...

You can retrieve and set the raw rich text from a rich text box. You could construct your own raw rich text if you learn a little about rtf format and assign it to the RichTextBox's Rtf property. For example, to insert your name into the RichTextBox you could maintain a raw rtf string that includes "{\colortbl ;\red0\green0\blue255;}" somewhere in the rtf (not sure exactly where, but this adds blue to the color table), and then insert this:

"\cf1\b" & Name & ": \cf0\b0" & Message

into the rtf in order to insert the message with the name in blue bold and message in black regular. Assign this to the richtextbox's Rtf property and... voila! Formatted rich text. Just insert your new messages in the rtf string like this, and assigning it to the richtextbox's rtf property.
 
For some reason it doesn't with Richtextboxes (well it doesn't in my case)

i know it works for regular textboxes.

the sendmessage thingy works perfectly
 
I am trying to do something similar. The only difference that I can see is that I need the whole line colored. For some reason the first one gets set correctly... the remaining lines seem to be set to the right color and THEN get changed to the same color as the first line... the last line added in my example code below seems to be the correct color. I am guessing that it has something to do with the selection part but I have struggled with this for hours and can't seem to get it. Any help is appreciated.

Code:
  string[] strTest = new string[] {"Line one", "Line two", "Line three", "Line four" , "Line five"};
  Color[] clrTest = new Color[] {Color.Red, Color.Blue, Color.Yellow, Color.Green, Color.Purple};
  
  int i = 0;
  
  foreach (string s in strTest)
  {
   rtb1.Text += s+Environment.NewLine;
   rtb1.SelectionStart = rtb1.Text.Length - (s.Length+1);
   rtb1.SelectionLength = s.Length;
   rtb1.SelectionColor = clrTest[i];
   rtb1.SelectionStart = rtb1.Text.Length;
   i++;
  }
 
Ok... after posting the above example it finally sunk in what Iceplug was saying... and the correcton Otani made. I got it to work with this code:


Code:
string[] strTest = new string[]{"Line one", "Line two", "Line three", "Line four", "Line five"};
Color[] clrTest = new Color[]{Color.Red, Color.Blue, Color.Yellow, Color.Green, Color.Purple};
int i = 0;
foreach (string s in strTest) {
 rtb1.SelectionColor = clrTest(i);
 rtb1.SelectedText = s + Environment.NewLine;
 i++;
}[size=2][color=#006400][/color][/size]


Or for the sake of completeness here is the VB example:

Code:
Dim strTest As String() = New String() {"Line one", "Line two", "Line three", "Line four", "Line five"}
Dim clrTest As Color() = New Color() {Color.Red, Color.Blue, Color.Yellow, Color.Green, Color.Purple}
Dim i As Integer = 0
For Each s As String In strTest
 rtb1.SelectionColor = clrTest(i)
 rtb1.SelectedText = s + Environment.NewLine
 i += 1
Next[size=2][color=#0000ff][/color][/size]
 
Back
Top