[2008] Finding text, and changing it
Ok, I am writing a sort of editor, and I am trying to implement simple Syntax highlighting. Basically all i want to do is search through a rich text box as it is typed into, and find and bold certain words, but i am unsure as to how i would do this. Thanks in advance.
Re: [2008] Finding text, and changing it
I think your dealing with a regex pattern. Here try to look at this thread and hope that helps.
Re: [2008] Finding text, and changing it
What you're specifically asking for is not that difficult but to actually implement syntax highlighting in a RichTextBox without your app grinding to a halt is a little more difficult. If you search the entire text every time then it will be all but unusable. Many people have asked about syntax highlighting before. I'd suggest a forum search to see what advice has been provided. I'd suggest searching the VB.NET forum too, as principles will be the same and code can be easily converted.
Re: [2008] Finding text, and changing it
Quote:
Originally Posted by fret
I think your dealing with a regex pattern. Here try to look at this
thread and hope that helps.
Regex doesn't seem to exist in C# 2008 :(!
Re: [2008] Finding text, and changing it
Quote:
Originally Posted by Seismosaur
Regex doesn't seem to exist in C# 2008 :(!
Of course it does. Go to the MSDN documentation and look up the Regex class. You'll then see which namespace it's a member of and which assembly it's declared in. If your project doesn't already have a reference to that assembly you can add one. You can then refer to the class qualified by its namespace or else import its namespace with a 'using' statement, just like every other class.
Re: [2008] Finding text, and changing it
Oh, ok. Sorry, I am kinda new to C#. How do i use the regular expressions though? :?
Re: [2008] Finding text, and changing it
You use as a filter of your every string pattern declaration to validate whether Alphabet, AlphaNumeric, Integer, Postive Integer, Floating point numbers. In terms it is used as a pattern to search a specific string. The one I'd give you the link gives a simple example so from there you can get an idea of it, but sad to say I'm no expert of these. :bigyello:
Re: [2008] Finding text, and changing it
That's not a one line answer. You should read the documentation for the Regex class first, to get a general idea of how you can use it assuming that you have a valid pattern. Then you should search the Web for regular expression tutorials, of which there would be several. They are not limited to .NET at all.
That said, if you're looking for key words then regular expressions is probably not appropriate. Regex is for finding patterns of characters, e.g. three letters followed by two numbers, but if know exactly what words you're looking for then that's not really what you need. You would probably just use IndexOf to find a substring. The question is, whatever you choose to do, when are you going to do it? Ideally you'd only look at the current word each time the text changes.
Re: [2008] Finding text, and changing it