Tuesday, February 17, 2009

Selecting a text pattern in the RichTextBox

Hi Friends,

There are lots of methods to select a Text pattern inside the RichTextbox body.
But When we put this RichTextbox inside a particular container(Eg:DataRepeater) we don't get the desired results.

This is due to the redraw and rendering of the DataRepeater.

For this I have a small solution.Here is the piece of Code for you;


private void Pattern_SelectionSenderDate(ref RichTextBox RTF_Box)
{
try
{
RTF_Box.DeselectAll();
RTF_Box.Refresh();
string strtempRTF_BoxText = RTF_Box.Text;
RTF_Box.Clear();

RTF_Box.Text = strtempRTF_BoxText;
int pos = 0;
string searchQuery = txtbxSearchBox.Text;
while (searchQuery.Length > 0)
{
pos = RTF_Box.Text.ToLower().ToString().IndexOf(searchQuery.ToLower(), pos);
if (pos == -1)
{
break;
}
RTF_Box.Select(pos, searchQuery.Length);
RTF_Box.SelectionColor = Color.FromArgb(153, 153, 153);
RTF_Box.SelectionBackColor = Color.Yellow;
RTF_Box.SelectionFont = new Font("Lucida Sans", (float)8.25,
System.Drawing.FontStyle.Bold);
pos = pos + 1;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}


Hope you like it.
Note: If you want to make this recipe more crispy,suggestions are always welcome :-)