|
-
May 24th, 2000, 09:51 PM
#1
Thread Starter
Hyperactive Member
Does anyone here know how to keep text in a richTextbox control to have its original coloring? to see what I mean check out the below code and explanation.
Here is an example of what I mean
Code:
Private Sub Command1_Click()
Dim x As Integer
If (color) Then
rtb.SelStart = 0
rtb.SelLength = Len(rtb.Text)
rtb.SelColor = vbRed
End If
MsgBox rtb.SelStart
If (color) Then
x = Len(rtb.Text)
rtb.Text = rtb.Text & " Yahoo "
rtb.SelStart = x
rtb.SelLength = 7
rtb.SelColor = vbBlue
Else
x = Len(rtb.Text)
rtb.Text = rtb.Text & " Yahoo "
rtb.SelStart = x
rtb.SelLength = 7
rtb.SelColor = vbGreen
End If
color = False
End Sub
Private Sub Form_Load()
color = True
End Sub
So when I click the command once, it colors the beginning text of the rtb to red, then it adds the word yahoo and colors it blue. But when I click it again, it adds the old text and covers the it all red, then it adds the second yahoo and colors it green. But I lost the original color of blue on the first yahoo.
-
May 24th, 2000, 10:29 PM
#2
Fanatic Member
Of course it did, That's what this code does!
Here's the flow
- click button
- The first 'if' sets all text red
- The second if else follows the if part, adds "yahoo" to the end and coloUrs (I'm an Aussie ) it (yahoo) blue
-click again
-Len(rtb.Text) now includes the previous yahoo so it, along with all the other text get's coloured red
-Then we follow the else path, we add yahoo again onto the text and colour it (the last yahoo) green
if you want to leave the first yahoo blue then you need a public variable with the old length, then only change to red up to there, then change to green from the end of the new length.
Please, next time a few more code comments, I'm not that good at reading other's code without them.
Cheers
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
May 24th, 2000, 10:51 PM
#3
Thread Starter
Hyperactive Member
That wont work though, I am going to be adding text as it comes in, and I need to be able to have all the old retain its color. Did I explain that good enough?
Let me try a situation that maybe my situation could apply to.
Say, I was making a chat server, where 5 people were on, and they all had different coloured (is that right austrailian?) So when a client recieves information it first would receive a colour, then the text, so I cant really keep track of the location or size for every time someone types something, for that is poor efficency. But how will the rich text box keep the colouring for the correct text? (Hmm, I think I just confused myself on that explanation )
Do you see what I am trying to say/do? If not I will try again to explain it.
-
May 24th, 2000, 11:07 PM
#4
Addicted Member
If..
If you are working on a chatapp, could you please mail it to me ([email protected]). I was doing the same thing for a couple of months ago, but i didn't make it work!
-
May 24th, 2000, 11:22 PM
#5
Fanatic Member
Give this a whirl
I know this is not the best solution in the world, but it works. I have tried a couple of other methods but to no avail.
The reason i could not get it to work is that when you re-set the text with RichText1.Text = RichText1.Text & myStr it is not like typing in a few new characters, it is infact like re-typing the whole thing.
Thsi gave me the clue that i needed. use SendKeys. After crashing my computer several times, i mean really crashing i have it working.
Code:
Option Explicit
Private Sub Command1_Click()
Dim x As Integer, y As Integer, z As Integer
Dim myStr As String
Randomize
x = Int(255 * Rnd)
y = Int(255 * Rnd)
z = Int(255 * Rnd)
'set the text to a random colour.
RichTextBox1.SelColor = RGB(x, y, z)
'just a demo string
myStr = Chr(13) & "User " & x & " : Hello"
'set the focus
RichTextBox1.SetFocus
'set the cursor to the end of the text box.
RichTextBox1.SelStart = Len(RichTextBox1.Text)
'send the string to the text box.
SendKeys myStr
End Sub
Hope this hepls
Regards
Iain, thats with an i by the way!
-
May 25th, 2000, 02:25 AM
#6
Thread Starter
Hyperactive Member
Iain you little programming god you.
it works beautifully!!!!!!!!!!!
Thank you
[Edited by billrogers on 05-25-2000 at 04:26 PM]
-
May 25th, 2000, 08:55 PM
#7
Thread Starter
Hyperactive Member
Does anyone know why this doesnt work? It is just a slight variation from Ian's example.
Code:
Private Sub addServerText(ByVal x As Integer, ByVal y As Integer, _
ByVal z As Integer, ByVal myStr As String)
' set the text to the selected color
rtbReceive.SelColor = RGB(x, y, z)
myStr = myStr & Chr(13)
' set the focus
rtbReceive.SetFocus
' set the cursor to the front of the text box
rtbReceive.SelStart = 0
' unlock the text bock
rtbReceive.Locked = False
'DoEvents
SendKeys myStr
DoEvents
rtbReceive.Locked = True
txtSend.SetFocus
End Sub
This doesnt switch colors, for some reason it stays with the first color it gets and continues to use that color even though, i set the color differntly when this is called.
[Edited by billrogers on 05-26-2000 at 10:00 AM]
[Edited by billrogers on 05-26-2000 at 10:00 AM]
-
May 25th, 2000, 09:06 PM
#8
Fanatic Member
Yep i do indeed know why.
When you set the colour, you set it for where the cursor is. So first move the cursor, then set the colour.
Code:
Private Sub addServerText(ByVal x As Integer, ByVal y As Integer, _
ByVal z As Integer, ByVal myStr As String)
myStr = myStr & Chr(13)
' set the focus
rtbReceive.SetFocus
' set the cursor to the front of the text box
rtbReceive.SelStart = 0
' set the text to the selected color
rtbReceive.SelColor = RGB(x, y, z)
' unlock the text bock
rtbReceive.Locked = False
'DoEvents
SendKeys myStr
DoEvents
rtbReceive.Locked = True
txtSend.SetFocus
End Sub
Easy hey
Iain, thats with an i by the way!
-
May 25th, 2000, 09:10 PM
#9
Thread Starter
Hyperactive Member
[Bill bows to Iain's greatness]
Thanks Iain. you da man
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|