Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rich TExt Box Control 1

Status
Not open for further replies.

sibasis

Technical User
Mar 1, 2002
94
I am using the richtextbox control to write status to this box, is there anyway I can highlight information lines from warning lines from error lines ?

 
I use a different SelectionColor. Suppose that you are always adding text to the end rather than trying to set existing text to a different color.

rtf.SelectionStart = 99999999 ' Set Start after all text
rtf.SelectionLength = 0 ' Already 0 because no text anyway
rtf.SelectionColor = color.Red ' Set whatever color
rtf.SelText = mytext ' Whatever text.

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Thanks John,

let me give you a detailed example
Say I have a rich textbox to which I am feeding lines of data on different system events, now some of these lines may be warning, error or plain information, now say the next line thats to be fed to the system is a warning line this line will have the color orange, the next line will have the color based on what type of line it is, lets assume 3 types of lines, information, warning and error for now.

Now I can change the color of the line already but when the next line is being added all lines change back to the default color or the rtb.

Its very simple to imagine, lemme know if you want me to put code here so that you can explain better I currently have 7 hours of rich text box experience so dont know much :eek:)
 
Show your code because if you did as I showed you, that should not happen, unless I messed/misssed up there (which ain't entirely unknown to happen so infrequently as to be considered rare). By using SELECTIONxxxx and SELECTEDTEXT, a change should only be effected upon the selected text.

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
there you go john :eek:)

Public Function DisplayLine(ByVal richtextbox1 As RichTextBox, ByVal smessage As String)

Dim start As Integer
Dim length As Integer

smessage = "[" + DateTime.Now.ToString + "] " + smessage

RichTextBox1.Text = RichTextBox1.Text + smessage + Chr(13) + Chr(10)

If InStr(smessage.ToUpper, "WARNING") <> 0 Then

length = Len(smessage)
richtextbox1.SelectionStart = Len(richtextbox1.Text) - length
start = richtextbox1.SelectionStart

richtextbox1.Select(start, length)
richtextbox1.SelectionColor = Color.Orange
richtextbox1.SelectionProtected = True

End If

If InStr(smessage.ToUpper, "ERROR") <> 0 Then
length = Len(smessage)
richtextbox1.SelectionStart = Len(richtextbox1.Text) - length
start = richtextbox1.SelectionStart
'richtextbox1.Find(smessage, RichTextBoxFinds.MatchCase)

richtextbox1.Select(start, length)
richtextbox1.SelectionColor = Color.Red
richtextbox1.Select(start + length, 1)
End If

iline = iline + 1

richtextbox1.Refresh()


End Function


This changes the color for the line but when the next line is added to the box the color of all the lines default back.
 
RichTextBox1.Text = RichTextBox1.Text + smessage + Chr(13) + Chr(10)

That is not what I showed you. There is no reason to take Text out of the RTF and put it back in. Just append text after setting up the RTF as to where, how long and what color should the text be. Also, you left out setting a color when it is just reguilar text.

Public Function DisplayLine(ByVal richtextbox1 As RichTextBox, ByVal smessage As String)

Dim clr as Drwawing.Color
Dim bProtect as boolean
smessage = "[" + DateTime.Now.ToString + "] " + smessage
bProtect = false
If InStr(smessage.ToUpper, "WARNING") <> 0 Then
clr = Color.Orange
bProtect = True
elseIfInStr(smessage.ToUpper, "ERROR") <> 0 Then
clr = Color.Red
else ' Need to reset
clr = Color.Black

End If
RichTextBox1.SelectionStart = 99999999 ' Put at end
richtextbox1.SelectionProtected = bProtect
RichTextBox1.SelectionColor = clr
RichTextBox1.SelectedText = smessage + Chr(13) + Chr(10)



Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
havent changed my code:eek:) this was my code before teh posting .. ur detailed msg will help :eek:)
will try it ... a star for your effort
I will let you know how it goes
thanks john.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top