jorge Posted July 23, 2003 Posted July 23, 2003 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 Quote Jorge - http://www.blackdot.be/?page=apache.htm
*Experts* mutant Posted July 23, 2003 *Experts* Posted July 23, 2003 You would have write syntax highlighting yourself or download/buy a ready control. RichTextBox doesnt have that functionality built in. Quote
*Experts* Volte Posted July 23, 2003 *Experts* Posted July 23, 2003 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. Quote
*Experts* mutant Posted July 23, 2003 *Experts* Posted July 23, 2003 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 Quote
jorge Posted July 23, 2003 Author Posted July 23, 2003 (edited) K thanx, I'll 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 Edited July 23, 2003 by jorge Quote Jorge - http://www.blackdot.be/?page=apache.htm
jorge Posted July 23, 2003 Author Posted July 23, 2003 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 Quote Jorge - http://www.blackdot.be/?page=apache.htm
*Experts* mutant Posted July 23, 2003 *Experts* Posted July 23, 2003 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 Quote
jorge Posted July 23, 2003 Author Posted July 23, 2003 euhm somting like: 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 ??? Quote Jorge - http://www.blackdot.be/?page=apache.htm
*Experts* Volte Posted July 23, 2003 *Experts* Posted July 23, 2003 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 Quote
jorge Posted July 23, 2003 Author Posted July 23, 2003 k have alook at it tomorrow, nover workt with loop be for do enay extra tips? Quote Jorge - http://www.blackdot.be/?page=apache.htm
*Experts* mutant Posted July 23, 2003 *Experts* Posted July 23, 2003 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. :) Quote
jorge Posted July 24, 2003 Author Posted July 24, 2003 (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 July 24, 2003 by jorge Quote Jorge - http://www.blackdot.be/?page=apache.htm
pjv Posted July 24, 2003 Posted July 24, 2003 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 Quote
Hamburger1984 Posted July 24, 2003 Posted July 24, 2003 the start variable should be in the function - because you don't need it somewhere else... and maybe you also should pass the RichTextBox into the Function to make it reusable anywhere... Quote
jorge Posted July 24, 2003 Author Posted July 24, 2003 ok, forget the array, i'll call it evry time with: Call Colorcoding("AddAltByEncoding", Color.RoyalBlue) But it there a way to make it case insensitive? Quote Jorge - http://www.blackdot.be/?page=apache.htm
AndreRyan Posted July 29, 2003 Posted July 29, 2003 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 Quote .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?
jorge Posted July 29, 2003 Author Posted July 29, 2003 Case in sensitif :cool: Quote Jorge - http://www.blackdot.be/?page=apache.htm
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.