Can't change RichTextBox Color Table.

T

Terry Olsen

I'm trying to change the color table of a RichTextBox. When the app starts,
it has the following RTF:
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0
A2like;}}
{\colortbl ;\red255\green255\blue0;}
\viewkind4\uc1\pard\fi-375\li375\cf1\f0\fs24\par
}

Calling the following code to alter the color table from Form1.Load:

Public Sub AddColorTable()
Dim x As String = txtChat.Rtf
Dim y As String = ""
Dim z As String = ""
Dim sr As New StringReader(x)
While sr.Peek > 0
z = sr.ReadLine
If z.ToLower.StartsWith("{\colortbl") Then
z = z.Replace(";}", RTFColors & "}")
End If
y &= z & vbCrLf
End While
sr.Close()
txtChat.Rtf = y
End Sub

Has no effect.

The routine works fine. Checking the 'y' variable just before assigning it
to txtChat.Rtf shows that it is correctly modified.

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0
A2like;}}
{\colortbl
;\red255\green255\blue0;\red255\green255\blue255;\red0\green0\blue0;\red255\green0\blue0;\red255\green128\blue0;\red255\green255\blue0;\red128\green255\blue0;\red0\green255\blue0;\red0\green255\blue128;\red0\green255\blue255;\red0\green128\blue255;\red0\green0\blue255;\red128\green0\blue255;\red255\green0\blue255;\red255\green0\blue128;\red192\green192\blue192;\red64\green64\blue64;\red128\green0\blue0;\red128\green64\blue0;\red128\green128\blue0;\red64\green128\blue0\red0\green128\blue0;\red0\green128\blue64;\red0\green128\blue128;\red0\green64\blue128;\red0\green0\blue128;\red64\green0\blue128;\red128\green0\blue128;\red128\green0\blue64;}
\viewkind4\uc1\pard\fi-375\li375\f0\fs24\par
}

Can anyone help?
 
P

Phill W.

Terry said:
I'm trying to change the color table of a RichTextBox.
Calling the following code to alter the color table from Form1.Load:
Public Sub AddColorTable()
Dim x As String = txtChat.Rtf
Dim y As String = ""
Dim z As String = ""
Dim sr As New StringReader(x)
While sr.Peek > 0
z = sr.ReadLine
If z.ToLower.StartsWith("{\colortbl") Then
z = z.Replace(";}", RTFColors & "}")
End If
y &= z & vbCrLf
End While
sr.Close()
txtChat.Rtf = y
End Sub

There's no guarantee that the colortbl entry starts on a new line;
indeed, I'd be surprised if it did.
You might be better off loading the whole RTF "string" in one go, then
replace the bit you're after, something like

Imports System.Text.RegularExpressions

Dim sr as New StringReader(x)
y = sr.ReadToEnd()
sr.Close()

y = Regex.Replace( y, "{\\colortbl.*}" _
, "{\colortbl " & RTFColors & ";}" )

txtChat.Rtf = y

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top