HI,
I have a rich text box and loaded a file in that. Used Find and replace form.
whaever text found i highlighted in the rich text box by giving background color, changing font size and giving color to the found text. say now i replaced the found text to another text. after replacing the found text it will search for another findings.
If i undo the changes first it chages the color of the replaced text, in second undo it changes the font size, third it changes the back ground color and fourth it changes to original text (found text). it take four undo to change or undo the text i have replaced.
My question is. is there is any way so i can do the all this four undo in one single undo process.
Thanks in advance.
public void Find()
{
textBox1.AutoCompleteCustomSource.Add(textBox1.Text);
nOmitTagEndPostion = richTextBox1.Text.Length;
if (Matchcase.Checked == true)
{
oFindCase = RichTextBoxFinds.MatchCase;
}
// to replace previous finding to its original status
if (nStartPostion < nOmitTagEndPostion)
{
if (nStartPostion != 0)
{
nStartPostion = nStartPostion - nEndPostion;
richTextBox1.Select(nStartPostion, nEndPostion);
nStartPostion = nStartPostion + nEndPostion;
richTextBox1.SelectionColor = Color.Black;
richTextBox1.SelectionBackColor = Color.White;
richTextBox1.SelectionFont = new Font("", 8.25F, System.Drawing.FontStyle.Regular);
}
nStartPostion = richTextBox1.Find(textBox1.Text, nStartPostion, nOmitTagEndPostion, oFindCase);
//nStartPostion = richTextBox1.Find(txtFind.Text, nStartPostion, RichTextBoxFinds.None, RichTextBoxFinds.None)
btnReplace.Enabled = false;
if (nStartPostion >= 0)
{
btnReplace.Enabled = true;
nEndPostion = textBox1.Text.Length;
richTextBox1.Select(nStartPostion, nEndPostion);
richTextBox1.ScrollToCaret();
//changing background, text size and text color in rich text box for found text
richTextBox1.SelectionColor = Color.Red;
richTextBox1.SelectionBackColor = Color.Yellow;
richTextBox1.SelectionFont = new Font("", 9.25F, System.Drawing.FontStyle.Bold);
nStartPostion += nEndPostion;
}
else
{
if (nStartPostion < -1 && nEndPostion ==0)
{
MessageBox.Show("Finished Searching the file. No such text found");
}
else
{
MessageBox.Show("Finished searching the document");
richTextBox1.ScrollToCaret();
richTextBox1.Select(0,0 );
nEndPostion = 0;
}
}
}
else
{
MessageBox.Show("Finished searching the document", "TagStyling", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (nStartPostion < 0)
{
nStartPostion = 0;
nEndPostion = 0;
btnReplace.Enabled = false;
richTextBox1.ScrollToCaret();
richTextBox1.Focus();
}
}
private void btnReplace_Click(object sender, EventArgs e)
{
if ((!string.IsNullOrEmpty(textBox2.Text)))
{
textBox2.AutoCompleteCustomSource.Add(textBox2.Text);
}
string sReplaceTextFull = string.Empty;
sReplaceTextFull = textBox2.Text;
nStartPostion -= nEndPostion;
nEndPostion = textBox2.Text.Length;
string sFindTag = string.Empty;
string sReplaceTemp = "\"\"";
if ((textBox2.Text.Contains("\\n")))
{
string sReplacetext = textBox2.Text.Replace("\\n", sReplaceTemp);
nEndPostion = textBox2.Text.Length;
sReplaceTextFull = sReplacetext;
}
richTextBox1.SelectedText = sReplaceTextFull;
richTextBox1.Select(nStartPostion, nEndPostion);
richTextBox1.ScrollToCaret();
nStartPostion += nEndPostion;
richTextBox1.SelectionColor = Color.Red;
Find();
button1.Focus();
label3.Visible = true;
}
private void Undo_Click(object sender, EventArgs e)
{
if(richTextBox1.CanUndo == true)
{
int count;
count = richTextBox1.Text.Length;
richTextBox1.Undo();
richTextBox1.ScrollToCaret();
}
}