tags:

views:

1462

answers:

4

Hi, My question has two parts:

1) How can I search for a sentence (e.g., Dell Canada) in a string (e.g., I am working in Dell Canada, and I found it...) .

2)The second part is my string is text in a RichTextBox, so I would like to find the TextRange of that selected sentence and apply certain decoration.

thanks.

+1  A: 

Part 1:

if (myString.IndexOf("Dell Canada") > -1)
{
    // do something great;
}
ck
+1  A: 

Part 1.

bool cntns = "I am working in Dell Canada, and found it ...".Contains("Dell Canada")
Matt Lacey
+5  A: 

Give this a whirl, it will set it to bold. There are many Selection... properties on the RichTextBox that you can use, also note that it is a case insensitive search:

    string textToSearchFor = "Dell Canada";
    int index = richTextBox1.Text.IndexOf(textToSearchFor, StringComparison.OrdinalIgnoreCase);
    if (index >= 0)
    {
        richTextBox1.Select(index, textToSearchFor.Length);
        richTextBox1.SelectionFont = new Font("Arial", 12f, FontStyle.Bold);
    }
    else
    {
        // not found
    }
John JJ Curtis
Thanks Jeff for the quick reply. Though I meant RichTextBox in Windows.Control rather than Windows.Form. I am using this control in WPF. Therefore, there is no richTextBox1.Select(index, textToSearchFor.Length); in it, but I got your concept, and I just need to look for a way to get that range. link : http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx
Does this article help you? : http://shevaspace.blogspot.com/2007/11/how-to-search-text-in-wpf-flowdocument.htmlHere's how to apply formatting to a TextRange: myTextRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
John JJ Curtis
This url has what you are looking for if you don't want to use rtf codes: http://www.cnblogs.com/Files/sheva/SyntaxHighlightingDemo.zip
John JJ Curtis
A: 

The first part is pretty simple as CK has pointed out. Rich text formatting is dictated by certain predefined codes as defined in the RTF specification. First get the underlying RTF raw string from the control using the RTF property

string rawString = richTextBox.Rtf;

For eg: the rtf for the phrase 'hello Bobby' will look like this. It is something like HTML, you have tags which define the formatting.

"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs17 hello Bobby\\par\r\n\\par\r\n}\r\n"

Now suppose I want to make the phrase bold, I would set the Rtf property by replacing the string with

"{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Microsoft Sans Serif;}}\r\n\\viewkind4\\uc1\\pard\\f0\\fs17
\\b hello Bobby\\par\r\n\\par\r\n}\r\n"

Note the \\b before the phrase. That's the code to make a given text bold.

To perform this formatting using code, find the string you want to format(using the first technique) and insert the rtf code in the required position. Hope this helps.

For the codes refer MSDN http://msdn.microsoft.com/en-us/library/aa140277.aspx

PS: Jeff's version is the easy one. This verison gives you infinite control. If you can do something in WordPad, you can do the same using rtf codes.

Bobby Alexander
Hi,How can I insert my rtf code in the required position? I guess I did not mention that I am using System.Windows.Control.RichTextBox in WPF.
I dont think it matters that you are using WPF. The property should be available.Well, you get the position of the text you want to modify and perform an InsertAt() or you can construct the RTF string dynamically and assign it to the Rtf property.
Bobby Alexander