Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted
You would have write syntax highlighting yourself or download/buy a ready control. RichTextBox doesnt have that functionality built in.
  • *Experts*
Posted

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.

  • *Experts*
Posted

Here is an example of what VolteFace said:

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

Posted

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)

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

  • *Experts*
Posted

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:

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:

If start = -1 Then Return

With:

If start = -1 Then Exit Do

  • *Experts*
Posted

Instead of Len("AddIcon") you should use the supported .NET way:

"AddIcon".Length

(At least I think that will work)

 

Also, you should restructure your Do Loop so it doesn't need to use Exit Do.

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

  • *Experts*
Posted

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.

:)

Posted (edited)

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 ----

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

Problem is, txtconf can't be found, code is in a module, and

frmMain.txtconf doesn't 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

Edited by jorge
Posted

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 (I'm 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

Posted

This should do it

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

.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...