colercoding

jorge

Well-known member
Joined
Jul 13, 2003
Messages
239
Location
Belgium
Hey,
I want to know if it is pasible to open a text file in a richtextbox,
and the give the word "Add type" a red color if so how can i do it?
thanx in advance greets
 
You would have write syntax highlighting yourself or download/buy a ready control. RichTextBox doesnt have that functionality built in.
 
It does have coloring ability though. You could simply use RTB.Text.IndexOf() to find each instance of the phrase you want to color, and then use RTB.SelectionStart, RTB.SelectionLength, and RTB.SelectedColor to change the color.

More information on this in your MSDN.
 
Here is an example of what VolteFace said:
Code:
Dim start As Integer = 0
Do
    start = r.Text.IndexOf("Add type", start)
    If start = -1 Then Return if not found the value will be -1
    r.SelectionStart = start
    r.SelectionLength = 8
    r.SelectionColor = Color.Red
    start += 8
Loop
 
K thanx,
Ill have a look at it tomorrow,
litle bit sick at the moment :(
well thanx, you guys are euhm how do i spell it gienuses!?
btw: is it case sensitive?s
 
Last edited by a moderator:
Ok i found out it is CaseSensitive any work around?
and why will it only od the first item?(sry noomb at loops and richtextboxes)
[VB]
If sColorcode = 1 Then
Dim start As Integer = 0
Do
start = txtconf.Text.IndexOf("AddIcon", start)
If start = -1 Then Return if not found the value will be -1
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("AddIcon")
txtconf.SelectionColor = Color.RoyalBlue
start += 8
Loop
Do
start = txtconf.Text.IndexOf("AddIconByEncoding", start)
If start = -1 Then Return if not found the value will be -1
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("AddIconByEncoding")
txtconf.SelectionColor = Color.RoyalBlue
start += 8
Loop
Do
start = txtconf.Text.IndexOf("AddIconByType", start)
If start = -1 Then Return if not found the value will be -1
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("AddIconByType")
txtconf.SelectionColor = Color.RoyalBlue
start += 8
Loop
Do
start = txtconf.Text.IndexOf("AcceptMutex", start)
If start = -1 Then Return if not found the value will be -1
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("AcceptMutex")
txtconf.SelectionColor = Color.RoyalBlue
start += 8
Loop
Do
start = txtconf.Text.IndexOf("AcceptPathInfo", start)
If start = -1 Then Return if not found the value will be -1
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("AcceptPathInfo")
txtconf.SelectionColor = Color.RoyalBlue
start += 8
Loop
Do
start = txtconf.Text.IndexOf("AcceptFileName", start)
If start = -1 Then Return if not found the value will be -1
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("AcceptFileName")
txtconf.SelectionColor = Color.RoyalBlue
start += 8
Loop
Do
start = txtconf.Text.IndexOf("Action", start)
If start = -1 Then Return if not found the value will be -1
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("Action")
txtconf.SelectionColor = Color.RoyalBlue
start += 8
Loop
Do
start = txtconf.Text.IndexOf("AddAlt", start)
If start = -1 Then Return if not found the value will be -1
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("AddAlt")
txtconf.SelectionColor = Color.RoyalBlue
start += 8
Loop
Do
start = txtconf.Text.IndexOf("AddAltByEncoding", start)
If start = -1 Then Return if not found the value will be -1
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("AddAltByEncoding")
txtconf.SelectionColor = Color.RoyalBlue
start += 8
Loop
Do
start = txtconf.Text.IndexOf("AddAltByType", start)
If start = -1 Then Return if not found the value will be -1
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("AddAltByType")
txtconf.SelectionColor = Color.RoyalBlue
start += 8
Loop
Do
start = txtconf.Text.IndexOf("AddCharset", start)
If start = -1 Then Return if not found the value will be -1
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("AddCharset")
txtconf.SelectionColor = Color.RoyalBlue
start += 8
Loop
Do
start = txtconf.Text.IndexOf("AddDefaultCharset", start)
If start = -1 Then Return if not found the value will be -1
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("AddDefaultCharset")
txtconf.SelectionColor = Color.RoyalBlue
start += 8
Loop
Do
start = txtconf.Text.IndexOf("AddDescription", start)
If start = -1 Then Return if not found the value will be -1
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("AddDescription")
txtconf.SelectionColor = Color.RoyalBlue
start += 8
Loop
Do
start = txtconf.Text.IndexOf("AddEncoding", start)
If start = -1 Then Return if not found the value will be -1
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("AddEncoding")
txtconf.SelectionColor = Color.RoyalBlue
start += 8
Loop
Do
start = txtconf.Text.IndexOf("AddHandler", start)
If start = -1 Then Return if not found the value will be -1
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("AddHandler")
txtconf.SelectionColor = Color.OrangeRed
start += 8
Loop
End If
[/VB]
 
The code I showed you highlights all occurences of the string, it probably doesnt highlight it in your case becuase you never reset the start variable to 0 to search the whole thing for each string.
Also you you have to change this line for every string:
Code:
start += 8 change this number to the length of your string.
With the length of your string.
And becuase you are doing more than one loop you have to substitute this:
Code:
If start = -1 Then Return
With:
Code:
If start = -1 Then Exit Do
 
euhm somting like:
[VB]
Dim start As Integer = 0
Do
start = txtconf.Text.IndexOf("AddIcon", start)
If start = -1 Then Exit Do
txtconf.SelectionStart = start
txtconf.SelectionLength = Len("AddIcon")
txtconf.SelectionColor = Color.RoyalBlue
start += Len("AddIcon")
Loop

[/VB]
???
 
Instead of Len("AddIcon") you should use the supported .NET way:
Code:
"AddIcon".Length
(At least I think that will work)

Also, you should restructure your Do Loop so it doesnt need to use Exit Do.
Code:
Do While start >= 0
                start = txtconf.Text.IndexOf("AddIcon", start)
                If start >= 0 Then
                    txtconf.SelectionStart = start
                    txtconf.SelectionLength = "AddIcon".Length
                    txtconf.SelectionColor = Color.RoyalBlue
                    start += "AddIcon".Length
                End If
            Loop
 
Thank you for correcting me VolteFace, I didnt have VS.NET available to me at the time I was writing that and so I wasnt sure of the loop.
:)
 
small related question,
Can i make a function out if it?
So i only have to add all the words to a arrey,
And then tell the funtion to fill in the words in the correct place?
Sind teh word list is 9,5 pages that would make a hole lot of code.
Thanx agen

---- Edit ----
[VB]
Dim start As Integer = 0
Sub Coloercodeing(sWord, sColor)
Do While start >= 0
start = txtconf.Text.IndexOf(sWord, start)
If start >= 0 Then
txtconf.SelectionStart = start
txtconf.SelectionLength = sWord.Length
txtconf.SelectionColor = Color.sColor
start += sWord.Length
End If
Loop
start = 0
end sub
[/VB]
Problem is, txtconf cant be found, code is in a module, and
frmMain.txtconf doesnt work anymore :/
and u need to call the function for every word, so is there a wat to put all the stuff in a array and pass it?

greets
 
Last edited by a moderator:
Either put the function in the same class as the richtextbox (so it can use it directly) or pass the reference of the richtextbox to the function. Also, start should probably be a local variable (Im not used to VB so I might be reading it wrong).

You could pass an array of strings and a corresponding array of colours, or you could put that info into a new class and pass an array of that.

Pete
 
the start variable should be in the function - because you dont need it somewhere else... and maybe you also should pass the RichTextBox into the Function to make it reusable anywhere...
 
ok, forget the array, ill call it evry time with:[VB]
Call Colorcoding("AddAltByEncoding", Color.RoyalBlue)[/VB]
But it there a way to make it case insensitive?
 
This should do it
Code:
Sub Colorcodeing(ByVal sWord as String, ByVal sColor as Color)
            Dim start As Integer = 0
            Do While start >= 0
                start = txtconf.Text.ToLower.IndexOf(sWord.ToLower, start)
                If start >= 0 Then
                    txtconf.SelectionStart = start
                    txtconf.SelectionLength = sWord.Length
                    txtconf.SelectionColor = Color.ToArgb
                    start += sWord.Length
                End If
            Loop
End Sub
 
Back
Top