In my application i have to highlight some text (incorrect words) which are returned by an API to show in a multiline textbox.
How to highlight specific words in the string returned by an API.
My code is
ServiceReference1.GetTextSoapClient c = new GetTextSoapClient();
string text = c.GetTextFromImage(#"D:\Files\OCR\" + FileUpload1.FileName);
txtContent.Text = text;
List<string> list_Words = GetWords(text);
How to highlight specific words in text.
if you just want to select a word in a multiline text box you must first find the word :
int start_index = textBox1.Text.IndexOf("word");
then highlight it :
textBox1.Focus();
textBox1.Select(start_index, word lenght);
but if you want to select multiple words and change the color , highlight background etc.... you must use rich text box
Related
i want to add formatted text and table to a richTextBox.
For this I use these codes:
Text:
richTextBox1.SelectionFont = new Font("Maiandra GD", 30, FontStyle.Bold);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SelectionIndent = 0;
richTextBox1.AppendText("text text text");
And the table:
StringBuilder tableRtf = new StringBuilder();
tableRtf.Append(#"{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}");
for (int j = 0; j <5; j++)
{
tableRtf.Append(#"\trowd");
tableRtf.Append(#"\cellx2500" + " ghhghgjghjghjhggjh");
tableRtf.Append(#"\intbl\cell");
tableRtf.Append(#"\cellx10000\intbl\cel");
tableRtf.Append(" " + "sdfsdfs" + #"\intbl\clmrg\cell\row");
}
tableRtf.Append(#"\pard");
tableRtf.Append(#"}");
richTextBox1.Rtf=tableRtf.ToString();
But the
richTextBox1.Rtf=tableRtf.ToString();
kills the previous contents.
How can I make compatible them?
It is not a duplicate because I want two thing:
1) add formatted text to richTextBox this way:
richTextBox1.SelectionFont = new Font("Maiandra GD", 30, FontStyle.Bold);
richTextBox1.SelectionColor = Color.Red;
richTextBox1.AppendText("text text text");
It is well readable and I can modify easily.
2) And I want to add tables.
So the structure:
text text text text text
text text text text text
|TABLE|
text text text text text
text text text text text
text text text text text
|TABLE|
etc.
But I don't know how can I apply tables without losing previous contents?
What you need to do is to dissect the rtf codes into the headers and the bodies.
The table body starts with the loop and keeping the \par is surely a good idea.
But you must neither replace the old text nor simply append the body to the end.
Instead you need to insert it before the last curly! This is because that last curly marks the end of the whole rtf code and anything after it will be ignored!
This was simple.
For a full solution you also will want to combine the headers.
This is a lot more work and writing it all out would go beyond an SO answer.
But the basic principle is simple:
You need to understand the things your table header adds to the things already in the primal header.
The most common things are afont table and a color table.
So if you want to use one or more different fonts in the appended table you need to do this:
add them to the font table with a new font index, e.g. as \f1\fnil Consolas; after the previous semicolon.
use it by changing the loop to include the new font right after the first \intbl table-paragraph-formatting control word: \intbl\f2\fs24 ghhghgjghjghjhggjh.
repeat as needed if you want to use different fonts in the table.
add a cfNfont color selector, if you want to.
The same idea will also work for the color table. It doesn't have a explicit indexing, so order matters; all colors are appended, each with a semicolon at the end:
{\colortbl ;\red255\green0\blue0;\red25\green0\blue220;}
..adds a blue color to the red from the formatted text.
You see, this is work and takes some effort and preparations.
You can find the full rtf specs here.
Here is a screenshot of playing a little with the rtf..:
Note that the parts of table header was created by the control; you may want to use a dummy control for this or maybe you can figure out which parts you need and which are not necessary..
Update: Here is a 'appending rtf for dummies' version:
tableRtf.Append(#"{\fonttbl{\f0\fnil\fcharset0 Courier;}}");
for (int j = 0; j <5; j++)
{
tableRtf.Append(#"\trowd");
tableRtf.Append(#"\cellx2500" + " ghhghgjghjghjhggjh");
tableRtf.Append(#"\intbl\cell");
tableRtf.Append(#"\cellx10000\intbl\cel");
tableRtf.Append(" " + "sdfsdfs" + #"\intbl\clmrg\cell\row");
}
tableRtf.Append(#"\pard");
tableRtf.Append(#"}");
string rtf1 = richTextBox1.Rtf.Trim().TrimEnd('}');
string rtf2 = tableRtf.ToString();
richTextBox1.Rtf = rtf1 + rtf2;
Note that the font table inserted before the table body does work! But make sure not to add the rtf-start tag!!
How do I place the cursor and highlight a word programmatically?
For example, in a WPF TextBox -> gTBxInfo.Text = "this is the fox and the cat". I want to highlight the word "fox".
int iIdx = stInfo.IndexOf("fox") finds the location, and int iLength = 3 has the length.
In real life, I don't know what was entered in the TextBox. When the customer enters a word (such as "fox") in another text box, the word "fox" needs to be highlighted in the gTBxInfo TextBox.
I've tried:
gTBxInfo.SelectionStart = iIdx;
gTBxInfo.SelectionLength = iLength;
You are just missing one line of code:
gTBxInfo.SelectionStart = iIdx;
gTBxInfo.SelectionLength = iLength;
gTBxInfo.Focus();
You need to set the focus on the TextBox before the highlight appears.
A Windows Phone Question
C#
I have a TextBox named "Search" and another TextBox named "Content". If the user adds text into the "Content" TextBox, is there a way, using the "Search" TextBox to search what the user typed in the "Content" TextBox and highlight that specific text?
e.g. When the user wants to search an app from the App List of the phone, It highlights and shows that particular app or any app containing that text.
If anyone has a solution
please let me know :)
To select text in a TextBox, this question provides the answer, basically:
//To select all text
textbox.SelectionStart = 0;
textbox.SelectionLength = textbox.Text.Length;
There's only a bit more logic to apply to achieve what you want. First get the input from the Content textbox and find the index of this value in the text in the Search textbox. If the value exists (thus the index being greater than -1), you can set the SelectionStart and SelectionLength.
string content = Content.Text;
int index = Search.Text.IndexOf(content);
if(index > -1)
{
Search.SelectionStart = index;
Search.SelectionLength = content.Length;
}
Update:
I tried following code in a WPF solution and it worked so normally this should work too for Windows Phone.
SearchTextBox is a TextBox
Content is a TextBlock
Just that you know what everything is in the code:
var regex = new Regex("(" + SearchTextBox.Text + ")", RegexOptions.IgnoreCase);
if (SearchTextBox.Text.Length == 0)
{
string str = Content.Text;
Content.Inlines.Clear();
Content.Inlines.Add(str);
}
else
{
//get all the words from the 'content'
string[] substrings = regex.Split(Content.Text);
Content.Inlines.Clear();
foreach (var item in substrings)
{
//if a word from the content matches the search-term
if (regex.Match(item).Success)
{
//create a 'Run' and add it to the TextBlock
Run run = new Run(item);
run.Foreground = Brushes.Red;
Content.Inlines.Add(run);
}
else //if no match, just add the text again
Content.Inlines.Add(item);
}
}
I'm using a Xceed.Wpf.Toolkit.RichTextBox that displays text saved in RTF. I've got a contextual menu that add some multi lines text at the caret position using this code
var text = "Line1" + Environment.NewLine + "Line2"
richTextBox.CaretPosition.InsertTextInRun(text);
It displays as I expect
Line1
Line2
When I save and reload the text (saved in RTF), it displays as this:
Line1Line2
When I look in the RTF code, it is saved without the CR and LF...
Why my CR/LF vanished? What is the solution to insert multi line text at the caret position?
I found how to do:
var text = "Line1" + Environment.NewLine + "Line2"
richTextBox.Control.Selection.Text = text;
Set RichTextBox.AcceptsReturn to True
http://social.msdn.microsoft.com/Forums/vstudio/en-US/f044f15b-48a4-485c-91c0-07a0828acb98/acceptsreturn-in-a-wpf-richtextbox
I am trying to input a predefined string into a RichTextBox during the process of a method, e.g.:
"various words" = RichTextBox1,ToString;
How can this be done?
You need to modify the Text property.
Example:
RichTextBox1.Text = "various words";
If you need the text to be richly formatted, you need the Rtf property instead.
Example:
RichTextBox1.Rtf = #"{\rtf1\ansi Hello in \b bold-face \b0 .}";