Highlight text in Windows 8 TextBox - c#

I'm looking for a way to highlight (not select) words based on search terms inside a Windows.UI.Xaml.Controls.TextBox control.
There doesn't seem to any way to override the text rendering behaviour? Can this be done with the textbox control?
Edit:
I was originally trying to use the RichEditBox but was having a problem with being able to paste in formatted text which I was trying to prevent (the only event I can clear the formatting on is TextChanged which seems a little late). I also really need more control over the rendering of the highlights

Textboxes are very limited in regards to formatting and text selection.
You might want to use the RichTextBox control rather than a TextBox as it gives you greater selection and formatting capabilities to make highlighting multiple terms easier.
Here's a quick start: http://www.devx.com/dotnet/Article/34644

I am having the same issue. But Incase you didn't know, I'll post this anyway (it's not a complete answer but might get you started.)
You can highlight words by using:
int indexOfFoundKeyword = 1;
int lastIndexOfFoundKeyword = 12;
txtbox.SelectionStart = indexOfFoundKeyword;
txtbox.SelectionLength = lastIndexOfFoundKeyword;
If you place this inside a button click event and then type about 20 chars and click the button you'll see that you can highlight words in a Metro textbox. The problem that I'm having is getting the start and ending of the found keyword so that I know what values to assign to SelectionStart and SelectionLength and if I understand your question correctly, I think that's where you're stuck at, too.
Update
OK, this seems to be a little tempremental but works most of the time. (and accurate when it does work):
// find the word 'jason'.
string word = "jason";
int a, b;
a = genericBox.Text.IndexOf(word);
b = word.Length;
genericBox.SelectionStart = a;
genericBox.SelectionLength = b;
If all you are trying to do is find some text, then select it, then this proves that it can be done. Now I know this is not the best way but it's the only way I've been able to get something like this to work in a plain TextBox for metro apps.

Perhaps you could create a control based on the TextBox control or altogether create a custom control. See here and here for more help.

Related

RTF text font/color changing [duplicate]

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

Highlighting particular text ina WPF textbox and registering event to that text only

In my WPF application I am having some following type of textbox
So, In this textbox, the text is not written by user it is generated by program, i wish to Highlight the particular text "Enter String Value" and when user clicks on that particular highlighted text only then I wish to fire a event.
Can u guys give me some hint or overview to achieve this. I tried few tricks but can't get success.
Thanks in advance.
Use <RichTextBox> with <FlowDocument>. As I know, TextBox allow you to determinate font (color, size, etc.) only one time, and you wouldn't able to modify color of partial text in one TextBox
I think you can use Regex to get your match from your textbox, and then create Run objects using that match. You can set the Background of the match (runobject.Background = Brushes.Red)

Is it possible to place "hints" in Windows Phone 7 TextBoxes?

I just got started programming for Windows Phone 7 after programming on Android for about half a year now. In Android, when I wanted textual input from a user, I would put a "hint" in the text box which would tell the user what they should input. When the text box was selected the hint would disappear. So far I've only seen ways to set text inside text boxes. The problem with this is that the text does not disappear when the user selects the text box forcing the user to erase the currently existing text.
I did some Google searches and skimmed through relevant documentation as well as a book I have but I've not found an answer yet. Thank you very much in advance for your time answering my question.
What you're looking for is normally called a "Watermarked" textbox. It's quite simple to create a control which implements this functionality.
Here are links to a few versions of implementations of this:
http://blogs.msdn.com/b/arun/archive/2010/03/29/watermarked-text-box-for-windows-phone.aspx
http://www.windowsphonegeek.com/articles/WP7-WatermarkedTextBox-custom-control
http://www.danielmoth.com/Blog/Watermark-TextBox-For-Windows-Phone.aspx
http://weblogs.asp.net/jdanforth/archive/2010/09/17/silverlight-watermark-textbox-behavior.aspx
WatermarkedTextBox for Windows Phone 7?
http://www.silverlight-zone.com/2011/03/wp7-watermarkedtextbox-custom-control.html
http://watermarktextbox.codeplex.com/
As I understand you want something similar to the default text in the searchbox (upper right corner of stackoverflow.com).
You have a few options.
Bool to check if user press the TextBox for the first time.
private bool m_textBoxPressedFirstTime = false;
and inside the event (double click the TextBox)
if(!m_textBoxPressedFirstTime)
{
myTextBox.Text = String.Empty;
m_textBoxPressedFirstTime = true;
}
Define your own template with a diffrent visual state.
You could use some WPF examples
How do you implement default text for a search box in WPF?

Silverlight C# - Set selection in textbox via code?

I'm working on a spellcheck function for my app, and want to have the word that's currently being looked at highlighted. I'm tracking the char count as I loop through the words in the textbox, so I know where to set the selection at.
I've tried txtArticle.Select(0, 10); just as a test, as well as setting the txtArticle.SelectionStart and txtArticle.SelectionLength properties, but the textbox doesn't show anything highlighted. What's the dealio?
Actual code I've tried:
txtArticle.SelectionStart = charCount;
txtArticle.SelectionLength = checkedWord.Length;
as well as
txtArticle.Select(charCount, checkedWord.Length);
I've positively no idea what I'm doing wrong, unless you can't set what's selected in the TextBox via code, which I just can't imagine is the case. Is there perhaps some extra property that I need to set for the TextBox itself?
Thanks yet again!
-Sootah
Documentation on MSDN of TextBox.SelectionStart Property has an example that works. This states that programmatic text selection is actually supported in Silverlight.
Looks like something else is going wrong in your application. When do you call this code? Try calling it after everything is loaded, and rendered on screen. May be on a click of a button.
If above does not work, create a sample application/page and try to follow MSDN example. When you get it working, try to figure out why it doesn't work in your application.

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

Categories