wildarmsdave
asked on
Richtextbox colour
Hi. I have a 'messenger system' for work. Basically, the admin PC's append information to a text file on the network (newest text to the top) and the shop floor client PC's load and display this information on screen every minute (if there are any changes to the text file.)
I now have a requirement to have coloured text on the screen. When the admin PC's append to the text file, the data is now saved in .rtf format. The client PC's load the text and display it in .RTF format. This works fine. However, there is a problem....
If I send this.....it works fine....
AAAAA (text in blue)
BBBBBB (text in red)
CCCCC (text in green)
If I send this...
AAAAA (text in blue)
then send
BBBBBB (text in red)
then send
CCCCC (text in green)
..... only CCCCC (the text added last) appears on the client PC.
All the text is appended to the text file if I view it with notepad. However, If I open up the text file in word pad or MS Word, then the same thing happens (only CCCCC shows up.)
This is obviously being caused when I am appending the text to the text file and is something to do with the .RTF codes contained in it (see below.) Any ideas how I can get round this?
Working
{\rtf1\ansi\ansicpg1252\de ff0\deflan g1033{\fon ttbl{\f0\f nil\fchars et0 Tahoma;}}
{\colortbl ;\red255\green0\blue0;\red 0\green0\b lue255;\re d0\green25 5\blue0;}
\viewkind4\uc1\pard\cf1\b\ f0\fs24 AAAAAAAA\cf0
\par \cf2 BBBBBBBBB\cf0
\par \cf3 CCCCCCCCC\cf0
\par }
Only Displays top line..
{\rtf1\ansi\ansicpg1252\de ff0\deflan g1033{\fon ttbl{\f0\f nil\fchars et0 Tahoma;}}
{\colortbl ;\red0\green255\blue0;}
\viewkind4\uc1\pard\cf1\b\ f0\fs24 CCCCCC\cf0
\par }
{\rtf1\ansi\ansicpg1252\de ff0\deflan g1033{\fon ttbl{\f0\f nil\fchars et0 Tahoma;}}
{\colortbl ;\red0\green0\blue255;}
\viewkind4\uc1\pard\cf1\b\ f0\fs24 BBBBBB\cf0
\par }
{\rtf1\ansi\ansicpg1252\de ff0\deflan g1033{\fon ttbl{\f0\f nil\fchars et0 Tahoma;}}
{\colortbl ;\red255\green0\blue0;}
\viewkind4\uc1\pard\cf1\b\ f0\fs24 AAAAAA\cf0
\par }
I now have a requirement to have coloured text on the screen. When the admin PC's append to the text file, the data is now saved in .rtf format. The client PC's load the text and display it in .RTF format. This works fine. However, there is a problem....
If I send this.....it works fine....
AAAAA (text in blue)
BBBBBB (text in red)
CCCCC (text in green)
If I send this...
AAAAA (text in blue)
then send
BBBBBB (text in red)
then send
CCCCC (text in green)
..... only CCCCC (the text added last) appears on the client PC.
All the text is appended to the text file if I view it with notepad. However, If I open up the text file in word pad or MS Word, then the same thing happens (only CCCCC shows up.)
This is obviously being caused when I am appending the text to the text file and is something to do with the .RTF codes contained in it (see below.) Any ideas how I can get round this?
Working
{\rtf1\ansi\ansicpg1252\de
{\colortbl ;\red255\green0\blue0;\red
\viewkind4\uc1\pard\cf1\b\
\par \cf2 BBBBBBBBB\cf0
\par \cf3 CCCCCCCCC\cf0
\par }
Only Displays top line..
{\rtf1\ansi\ansicpg1252\de
{\colortbl ;\red0\green255\blue0;}
\viewkind4\uc1\pard\cf1\b\
\par }
{\rtf1\ansi\ansicpg1252\de
{\colortbl ;\red0\green0\blue255;}
\viewkind4\uc1\pard\cf1\b\
\par }
{\rtf1\ansi\ansicpg1252\de
{\colortbl ;\red255\green0\blue0;}
\viewkind4\uc1\pard\cf1\b\
\par }
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Apologies for the delay in awarding your points, I've been away for a few days.
This seems more trouble than it's worth now. I may be as well creating my own colour system eg
*&anything between these symbols are coloured red*&
Unless there is an easier way??
This seems more trouble than it's worth now. I may be as well creating my own colour system eg
*&anything between these symbols are coloured red*&
Unless there is an easier way??
You could just use an RGB encoding, use a character as a delimiter that wont be in your text, such as:
¶
then after each occurance of that, put an RGB value.
e.g.
¶FF0000
that would mean the next bit is in red..
This way, you know that after each ¶ there are 3 bytes, R G and B which you can use to set the text colour of the output :)
¶
then after each occurance of that, put an RGB value.
e.g.
¶FF0000
that would mean the next bit is in red..
This way, you know that after each ¶ there are 3 bytes, R G and B which you can use to set the text colour of the output :)
ASKER
Thanks for that. I don't suppose you could supply an example of how I decode this could you? I'll award more points for this of course. Thanks.
well, I have never worked on the decoding code for this, since i have only used this method to encode coloured/bold/underline text from a rich text box into IRC compatible code..
(I dont know if youve ever used IRC, but to generate colours etc you insert a special character followed by the number of the colour before the text)
This doesnt actually involve RGB, its just like 1,2,3 etc, but these values apply to rich text boxes anyway..
I will condense the code here into colours only:
'SPECIAL IRC CHARS
Const ColorChar = "¶" 'this is for your example
'VARIABLES
Dim TheColor As Variant
Dim PrevColor As Variant
Dim TheChar As String
Dim TTA As String
Private Sub cmdDo_Click()
RichTextBox2.Text = ""
For i = 0 To Len(RichTextBox1.Text)
TTA = ""
RichTextBox1.SelStart = i
RichTextBox1.SelLength = 1
TheChar = RichTextBox1.SelText
'GET THE COLOR RIGHT
TheColor = RichTextBox1.SelColor
If TheColor = PrevColor Then
'DONT RE-WRITE COLOR CODE
TTA = TTA & TheChar
Else
'WRITE THE NEW COLOR CODE
TTA = TTA & ColorChar
'find the color number
For c = 0 To 15
If TheColor = lblFG(c).BackColor Then
'found the color
TTA = TTA & c & TheChar
PrevColor = TheColor
Exit For
End If
Next c
End If
'ADD CHAR AND CODES TO TEXT
RichTextBox2.Text = RichTextBox2.Text & TTA
Next i
End Sub
So basically if you have edited some text in richtextbox1 to have colours etc, then in richtextbox2 the encoded version will appear..
I suppose the above code can be easily adapted to reverse this process :)
(I dont know if youve ever used IRC, but to generate colours etc you insert a special character followed by the number of the colour before the text)
This doesnt actually involve RGB, its just like 1,2,3 etc, but these values apply to rich text boxes anyway..
I will condense the code here into colours only:
'SPECIAL IRC CHARS
Const ColorChar = "¶" 'this is for your example
'VARIABLES
Dim TheColor As Variant
Dim PrevColor As Variant
Dim TheChar As String
Dim TTA As String
Private Sub cmdDo_Click()
RichTextBox2.Text = ""
For i = 0 To Len(RichTextBox1.Text)
TTA = ""
RichTextBox1.SelStart = i
RichTextBox1.SelLength = 1
TheChar = RichTextBox1.SelText
'GET THE COLOR RIGHT
TheColor = RichTextBox1.SelColor
If TheColor = PrevColor Then
'DONT RE-WRITE COLOR CODE
TTA = TTA & TheChar
Else
'WRITE THE NEW COLOR CODE
TTA = TTA & ColorChar
'find the color number
For c = 0 To 15
If TheColor = lblFG(c).BackColor Then
'found the color
TTA = TTA & c & TheChar
PrevColor = TheColor
Exit For
End If
Next c
End If
'ADD CHAR AND CODES TO TEXT
RichTextBox2.Text = RichTextBox2.Text & TTA
Next i
End Sub
So basically if you have edited some text in richtextbox1 to have colours etc, then in richtextbox2 the encoded version will appear..
I suppose the above code can be easily adapted to reverse this process :)
under that, you can prepend the paragraphs..
Also, you need to keep the RTF content open at the end of each \par (agraph) until the last one..
i.e.
{\rtf1\ansi\ansicpg1252\de
{\colortbl ;\red255\green0\blue0;} <--COLOUR TABLE
\viewkind4\uc1\pard\cf1\b\
\par } <-- END OF FINAL PARAGRAPH
then becomes:
{\rtf1\ansi\ansicpg1252\de
{\colortbl ;\red255\green0\blue0;} <--COLOUR TABLE
\viewkind4\uc1\pard\cf1\b\
\par <-- END OF A PARAGRAPH (notice no '}' )
\viewkind4\uc1\pard\cf1\b\
\par } <-- END OF FINAL PARAGRAPH
Hope that helps :)