RTF text font/color changing [duplicate] - c#

I want to create a simple editor like Notepad++ with simple functionality... I need to color a specific word in the rich text box area. How can I do that?
For example: when the user write these word, I want to color them to the blue color. These words are: for, while, if, try, etc.
How can I make the richtextbox to select a specific word and then color them?
And, if I want to make a comment and color everything after the //, how is that done in the richtextbox?
How do I number the line in the text box, so I can now the line number when I'm coding in my editor?

Here's some code you can build on in order to achieve the functionality you want.
private void ColourRrbText(RichTextBox rtb)
{
Regex regExp = new Regex("\b(For|Next|If|Then)\b");
foreach (Match match in regExp.Matches(rtb.Text))
{
rtb.Select(match.Index, match.Length);
rtb.SelectionColor = Color.Blue;
}
}
The CodeProject article Enabling syntax highlighting in a RichTextBox shows how to use RegEx in a RichTextBox to perform syntax highlighting. Specifically, look at the SyntaxRichtTextBox.cs for the implementation.

In general, you have to work on the selection in RichTextBox. You can manipulate the current selection using the Find method or using SelectionStart and SelectionLength properties. Then you can change properties of selected text using SelectionXXX properties. For example, SelectionColor would set the color of current selection, etc. So you have to parse text in richtextbox and then select part of texts and change their properties as per your requirements.
Writing a good text editor using RichTextBox can be quite cumbersome. You should use some library such as Scintilla for that. Have a look at ScintillaNet, a .NET wrapper over Scintilla.

Did you know that Notepad++ uses Scintilla?
You actually do not have to reinvent the wheel by going through all the trouble as there is a .NET port of Scintilla named ScintillaNET which you can freely embed in your application as the source code editor :)
But to answer your question, there are few parts that you need to understand
Finding what to color
When to color
How to color
For the first part, there may be different approaches, but I think using regular expressions would be a good choice. I am sorry, but I don't know regular expressions much so I cannot help you in that case.
When to color is very crucial and if you do it wrong, your application will suffer a heavy performance penalty. I suggest you refer to XPath Visualizer which was done by our own Stack Overflow member, Cheeso. Take a look at the source on how the coloring of the syntax was done. But if you ScintillaNET, everything would be taken care of. Anyway, I really can't seem to find this documentation where he clearly showed how the coloring of the text was done. I would most definitely post it here if I find it.
The third question I think is covered by VinayC. But basically you use SelectionColor along with SelectionStart.

here is a good link on c-sharpcorner.com website on basic richtextbox syntax highlighting. I assume that you and any one visiting this page for similar problem want to do it for learning purpose. But if any one wants to do it for some making some comercial level IDE then it must use scintilla or some similar.
An other approach for this is to directly change the RTF of the richtextbox. Look in codeproject.com there are lot of articles similar to this problem.

I had some problems with that and here is my solution, beats me why it has to be done like this, but it works:
// position on end of control...
richTextBox.UpdateLayout();
richTextBox.ScrollToEnd();
richTextBox.UpdateLayout();
// ...then select text and it will be position on top of control.
richTextBox.Focus();
richTextBox.Selection.Select(foundRange.Start, foundRange.End);
richTextBox.BringIntoView();

Vb.net implementation
Imports System.Text.RegularExpressions
Private Sub formatString()
Dim rg =New Regex("\b(for|while|if|try)\b")
Dim m As Match
For Each m In rg.Matches(RichTextBox1.Text)
RichTextBox1.Select(m.Index,m.Length)
RichTextBox1.SelectionColor=Color.Green
Next
End Sub

Related

What control to chose for large text?

What control should I choose in order to deposit a large amount of words with lots of line breaks and a way to highlight the current line. Like a debugger, if i am at the line x I want that line to be colored or something. I've added a small portion of code to give you an example and I also thought that a rich textbox should be good, any suggestions on what should I use ?(my program is a debugging simulator)
Example of what i want the text to look like (colour-wise),
#include <iostream>
using namespace std;
int a,b;
..........
return 0;
}
Check out the AvalonEdit control and its sample could be found here.
http://www.codeproject.com/Articles/42490/Using-AvalonEdit-WPF-Text-Editor
In WPF you should use RichTextBox as it gives you a wide area of customization options for its content.
Anyway, you should really think in the beginning about all the things you want to do in control, and if you don't find it satisfactory, you'll always have the option to create a custom control with the functionality you want.
I would highly recommend using Scintilla, which has a .NET port, and you can embed this control in WPF using the method mentioned here.
if you are interested in large amount of words, you should think about virtualization of data using the regular WPF ListView.
Refer to this great SO anwer for more information.
However, it sounds like you don't really intent to have large quantities of data, and in that case the other answers here will suit you better.

Windows-Store-App RichEditBox Markdown Highlighting

at the moment I'm trying to get into Windows Store App Development and I'm stuck at some point.
I want to implement sort of a "markdown language" like the one on stackoverflow to highlight certain parts of text input.
Besides that I want to give the user the ability to use different font colors on his text.
The RichEditBox seems to be the ideal control for this task, but I don't know how to detect markup entering on the fly.
For example when the user enters **Test** the text should be transformed to Test immediately.
I have tried to approach this by listening to the "TextChanged" event and looking if the user enters **. If this is the case and if he entered the sequence ** already one time before, then I'm setting the character format of the text range from the end of the first annotation sequence (start marker) to the beginning of the second (close marker) annotation to bold.
But this solutions seems to be very quick and dirty.
My second thought was to use the WebView control to render the text after preprocessing it with "Markdown Sharp".
But then the user won't be able to edit text.
So I need to get some advice or tip on approaching this problem. I also looked into writing a custom RichEditBox control, but I have no experience in custom control development and there aren't that many resources on the web for Windows 8 development for now.
Thanks in advance.
As I see it, your problem is that you want to edit the "source" based on Markdown syntax AND show the formatted result in the same place. How would you revert Test to regular, as long as the asterisks are gone? If the answer is "using a button" then why not use the button to make it bold in first place?
However, you could do a hybrid thing: apply formatting in the source text, while maintaining the Markdown markup (not sure if this is entirely doable for all Markdown tricks, though). That is, **Test** would look in the source like **Test**. For the final, formatted result you would use a separate view, such as RichTextBlock.
In order to do the hybrid formatting, an option would be to have a background thread matching regularly the whole text against regular expressions specific to the Markdown syntax. For each match the corresponding text range would then be formatted accordingly.

Drawing custom TextBox Controls

I found this code here which explains how to create custom controls. I have been using these controls as buttons, but now that I am beginning to understand the code a bit more, I would like to try and create a text box control using the same drawing techniques. I have been searching endlessly for some examples on the subject, but cannot find a single one. I don't understand how a textbox can be writable if a rectangle is being used to make it. Does anybody have any experience creating custom controls in C#? I would like for my textbox to be able to match the theme in the above link which is why it has to be custom made.
Well, at the beginning you should have in mind, that the implementation of new text box control is kinda complicate thing. It is necessary that you consider the following points:
1) What should your textbox do? How can the user interact with it? Which events do you necessarely throw?
2) How should you textbox be drawn? Is it a simple box with an outline or do you have elements which are different if the user gets interacted.
A good starting point for implementing your own textbox is to look how it works under the hood - in fact, you should start by referencing to samples which came from the DirectX and DirectDrawing area, one example is the following link (the sample is for c++, but the concepts are the same as used in windowsforms or wpf drawing):
http://www.uc-forum.com/forum/d3d-tutorials-and-source/65377-make-textbox-ingame-console-directx.html
a more direct sample (explaining howto extend an existing textbox) can be found here:
http://www.codedblog.com/2007/09/17/owner-drawing-a-windowsforms-textbox/
All in all, to achieve your goal try to extend the basic text box at the beginning and afterwards start with a component which is not that complicated, for example a simple checkbox. Go on and at the end you will be able to implement your own textbox control ;)

How to select text from the RichTextBox and then color it?

I want to create a simple editor like Notepad++ with simple functionality... I need to color a specific word in the rich text box area. How can I do that?
For example: when the user write these word, I want to color them to the blue color. These words are: for, while, if, try, etc.
How can I make the richtextbox to select a specific word and then color them?
And, if I want to make a comment and color everything after the //, how is that done in the richtextbox?
How do I number the line in the text box, so I can now the line number when I'm coding in my editor?
Here's some code you can build on in order to achieve the functionality you want.
private void ColourRrbText(RichTextBox rtb)
{
Regex regExp = new Regex("\b(For|Next|If|Then)\b");
foreach (Match match in regExp.Matches(rtb.Text))
{
rtb.Select(match.Index, match.Length);
rtb.SelectionColor = Color.Blue;
}
}
The CodeProject article Enabling syntax highlighting in a RichTextBox shows how to use RegEx in a RichTextBox to perform syntax highlighting. Specifically, look at the SyntaxRichtTextBox.cs for the implementation.
In general, you have to work on the selection in RichTextBox. You can manipulate the current selection using the Find method or using SelectionStart and SelectionLength properties. Then you can change properties of selected text using SelectionXXX properties. For example, SelectionColor would set the color of current selection, etc. So you have to parse text in richtextbox and then select part of texts and change their properties as per your requirements.
Writing a good text editor using RichTextBox can be quite cumbersome. You should use some library such as Scintilla for that. Have a look at ScintillaNet, a .NET wrapper over Scintilla.
Did you know that Notepad++ uses Scintilla?
You actually do not have to reinvent the wheel by going through all the trouble as there is a .NET port of Scintilla named ScintillaNET which you can freely embed in your application as the source code editor :)
But to answer your question, there are few parts that you need to understand
Finding what to color
When to color
How to color
For the first part, there may be different approaches, but I think using regular expressions would be a good choice. I am sorry, but I don't know regular expressions much so I cannot help you in that case.
When to color is very crucial and if you do it wrong, your application will suffer a heavy performance penalty. I suggest you refer to XPath Visualizer which was done by our own Stack Overflow member, Cheeso. Take a look at the source on how the coloring of the syntax was done. But if you ScintillaNET, everything would be taken care of. Anyway, I really can't seem to find this documentation where he clearly showed how the coloring of the text was done. I would most definitely post it here if I find it.
The third question I think is covered by VinayC. But basically you use SelectionColor along with SelectionStart.
here is a good link on c-sharpcorner.com website on basic richtextbox syntax highlighting. I assume that you and any one visiting this page for similar problem want to do it for learning purpose. But if any one wants to do it for some making some comercial level IDE then it must use scintilla or some similar.
An other approach for this is to directly change the RTF of the richtextbox. Look in codeproject.com there are lot of articles similar to this problem.
I had some problems with that and here is my solution, beats me why it has to be done like this, but it works:
// position on end of control...
richTextBox.UpdateLayout();
richTextBox.ScrollToEnd();
richTextBox.UpdateLayout();
// ...then select text and it will be position on top of control.
richTextBox.Focus();
richTextBox.Selection.Select(foundRange.Start, foundRange.End);
richTextBox.BringIntoView();
Vb.net implementation
Imports System.Text.RegularExpressions
Private Sub formatString()
Dim rg =New Regex("\b(for|while|if|try)\b")
Dim m As Match
For Each m In rg.Matches(RichTextBox1.Text)
RichTextBox1.Select(m.Index,m.Length)
RichTextBox1.SelectionColor=Color.Green
Next
End Sub

Editable Rich text box

Please Do any one know a free rich text box in C# like the one used in Microsoft Encarta, such that when i write x^2 it makes the 2 up the x with smaller font (like sup in HTML).
Thanks very much
The possibility to make text superscript or subscript is built-in in the richtextbox: see this msdn article.
Of course the base richtextbox does not do it "automatically", but you might try replacing text as the user types...
EDIT: If I understand correctly, you don't need just subscript and superscript capabilities, but a full equation editor. I don't have any direct experience with something like that, but just googling for ".net equation editor" yields some interesting results, like this equation editor in C#. Give it a try... I'm afraid that it won't be easy to find something that 1) does exactly what you need 2) is easy to use and 3) is free, but I wish you all the best luck :)
I don't know encarta do. But if you want some components for edit html try find:
http://www.fckeditor.net/
http://devexpress.com/Products/NET/Controls/WinForms/Editors/ (serach for a new editor control, can edit html)

Categories