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();
             }
        }

If this is a Windows Forms app then this is the only way.

If this is a WPF app then the RichTextBox (TextBoxBase) has BeginChange and EndChange methods to encapsulate multiple changes in to a single undo unit.

I argue that, no its not the only way. There is NEVER an "only way". As a programmer, finding ways to get around limitations is only the first step of reaching a goal.

But first, why do you need to change the font color and properties to show a selection? is not highlighting the selection with the select method not enough? I don't know why it wouldn't be.

But if you do want to change the font, and you are set on it. Then you have some key choices. You can implement your own stack and custom undo, which isn't that hard at all, just google it. its out there in a million places. Or you can custom draw your rich text box like it typically done for spell correction here is a link to how to do that Owner Drawing a Text Box. But those are only 2 of what could be a hundred possible solutions. you could even inherit from the rich text box and override some methods and add the begin and end change events, or decompile the code and add them and recompile it in your solution.

There is always a way. Here is 4 to try. Good luck and happy coding!

Thanks Diamonddrake.

First i searched text in rich text box using find and replace method. I like to keep the focus in find next button so that user press enter key it search for next findings. if i do that the found text not get highlighted in the rich text box. to highlight the text iam changing the back ground color of the founded text in the RTB (now iam just changing the background color not the font size and color of the text).

When i replace the found text it will replaced to new text and search for next findings. if next findings occurs it will change the back ground color.

when try to do undo option(my aim is it should undo the replaced text) but for first undo it replace the background color and next undo it replace the text.

How to achieve this is single undo.

Iam collecting all back gound starting position in a list and i replace all the collection to its original color when user close the find and replace form. like this i can collect all replace text in a list and do. but problem is when user typed text in rich text box. is there is a way to collect the typed text in rich text box.

Currently iam looking on the link u have giving.

Thanks once again.

I got what i expected....

private void btn_Find_Click(object sender, EventArgs e)
        {

            if (Checkbox_Wildfnd.Checked == true)
                FindWileCards(text);
            findflag = true;
            if (Tab_FndRep.SelectedIndex == 0)
            {
                text = TxtBox_Fnd.Text;
                if (text != "")
                {
                    Btn_Find.Enabled = true;
                    Btn_REpFndWhat.Enabled = false;
                }
                else
                {
                    Btn_Find.Enabled = false;
                }
            }
            else if (Tab_FndRep.SelectedIndex == 1)
            {
                text = TxtBox_RepFnd.Text;
                if (text != "")
                {
                    Btn_REpFndWhat.Enabled = true;
                    Btn_Find.Enabled = false;
                }
                else
                {
                    Btn_REpFndWhat.Enabled = false;
                }
            }
            if (Combo_Option.SelectedIndex == 0 || Combo_Option.SelectedIndex == 2)
            {
                if (nStartPostion < 0)
                    nStartPostion = 0;
                oFindCaseFnd = RichTextBoxFinds.None;
                if (Checkbox_fndRepMatchCase.Checked == true || Checkbox_fndMatchCase.Checked == true)
                oFindCaseFnd = RichTextBoxFinds.MatchCase;
                if (Checkbox_fndWholeWord.Checked == true || Checkbox_fndRepWholeWord.Checked == true)
                    oFindCaseFnd = oFindCaseFnd | RichTextBoxFinds.MatchCase;

                    if (text != "")
                    {
                        Find();
                    }
                
            }
            //else if (Combo_Option.SelectedIndex == 2)
            //{
            //    if (text != "")
            //    {
            //        //bibflag = false;

            //            position = Find(nStartPostion, nEndPostion = text.Length, text, oFindCaseFnd).Split(',');
            //            nStartPostion = Convert.ToInt32(position[0]);
            //            nEndPostion = Convert.ToInt32(position[1]);
                    
            //    }
            //}

            else if (Combo_Option.SelectedIndex == 1)
            {
                if (nStartPostion ==0)  
                nStartPostion = stapoint + nEndPostion;
                  Btn_Previous_Click(sender, e);
            }


          
            //if (!(nStartPostion <= 0 && nEndPostion == 0))
            //    btn_Find.Focus();


            //               int startindex = FindMyText(textBox1.Text.Trim(), start, richTextBox1.Text.Length);
            //               richTextBox1.Focus();
            //richTextBox1.SelectionColor = Color.Blue;
            //richTextBox1.ScrollToCaret();
            //               richTextBox1.SelectionStart = startindex;

            //               int endindex = textBox1.Text.Length;

            //               richTextBox1.SelectionLength = endindex;

            //               start = startindex + endindex;

        }


public void Find()
        {
            //fndtextBox.AutoCompleteCustomSource.Add(fndtextBox.Text);

            nOmitTagEndPostion = richTextBox1.Text.Length;
            //if (Matchcase.Checked == true)
            //{
            //    oFindCase = RichTextBoxFinds.MatchCase;
            //}
            //else
            //{
            //    oFindCase = RichTextBoxFinds.None;
            //}

            if (nStartPostion < nOmitTagEndPostion)
            {
                if (nStartPostion > -1 && nStartPostion < 0)
                    nStartPostion = 0;

                if ((nStartPostion != 0))
                {
                    //if (findflag == true)
                    nStartPostion -= nEndPostion;
                    richTextBox1.Select(nStartPostion, nEndPostion);
                    if (findflag == true)
                        nStartPostion = nStartPostion + nEndPostion;
                    //if (Checkbox_Highlight.Checked == false)
                    //{

                    //    richTextBox1.SelectionBackColor = Color.Lavender;

                    //}
                    //else
                    //{
                    //    // richTextBox1.SelectedText
                    //    richTextBox1.SelectionBackColor = Color.Yellow;
                    //}

                }
                stapoint = nStartPostion;
                nStartPostion = richTextBox1.Find(text, nStartPostion, nOmitTagEndPostion, oFindCaseFnd);
                
                //if (start != -1)
                //start = richTextBox1.Find(text, nStartPostion, RichTextBoxFinds.None, RichTextBoxFinds.None)
                    if (Tab_FndRep.SelectedIndex == 1)
                    {
                        if (TxtBox_RepFnd.Text == "")
                        {
                            Btn_Replace.Enabled = false;
                            btn_Replaceall.Enabled = false;
                        }
                    }

                    if (nStartPostion >= 0)
                {
                    if (Tab_FndRep.SelectedIndex == 1)
                    {
                        if (TxtBox_RepFnd.Text != "")
                        {
                            Btn_Replace.Enabled = true;
                            btn_Replaceall.Enabled = true;
                        }
                    }
                    
                    nEndPostion = text.Length;
                    richTextBox1.Select(nStartPostion, nEndPostion);
                    richTextBox1.Focus();
                    //if (nStartPostion >= 500)
                    //{
                    //    if (scrollflag == true)
                    //    {
                    //        scrollflag = false;
                    //        richTextBox1.ScrollToCaret();
                    //    }
                    //}
                    
                    //if (bibflag == true)
                    //    richTextBox1.SelectionBackColor = Color.GreenYellow;
                    //else
                    //    richTextBox1.SelectionBackColor = Color.BurlyWood;
                    //if (bibflag == false)
                    if (Tab_FndRep.SelectedIndex == 0)
                        TxtBox_Fnd.ForeColor = Color.Green;
                    else
                        TxtBox_RepFnd.ForeColor = Color.Green;
                    nStartPostion += nEndPostion;
                }
                else
                {
                    if (Tab_FndRep.SelectedIndex == 0)
                        TxtBox_Fnd.ForeColor = Color.Green;
                    else
                        TxtBox_RepFnd.ForeColor = Color.Green;
                        ////MessageBox.Show("Finished searching the document");
                        ////richTextBox1.Select(0, 0);
                        ////richTextBox1.ScrollToCaret();
                        nStartPostion = 0;
                        //richTextBox1.Refresh();
                    
                }
                if (flag == true)
                {
                    if (Checkbox_Highlight.Checked == true)
                    {
                        flag = false;
                        int a = nStartPostion;
                        nStartPostion = 0;
                        richTextBox1.Refresh();
                        do
                        {
                            if (nStartPostion >= 0)
                            {
                                nStartPostion = richTextBox1.Find(text, nStartPostion, nOmitTagEndPostion, oFindCaseFnd);

                                nEndPostion = text.Length;
                                if (nStartPostion != -1)
                                {
                                    if (nStartPostion != (a - nEndPostion))
                                    richTextBox1.SelectionBackColor = Color.Yellow;
                                Backcolorchanges.Add(nStartPostion);
                                nStartPostion += nEndPostion;
                                }
                            }

                        } while (nStartPostion > 0);
                        nStartPostion = a;
                        findflag = true;
                        if (Repallflag == false)
                        Find();
                        //Btn_Find.Focus();

                    }
                }
            }
            else
            {
                TxtBox_Fnd.ForeColor = Color.Red;
               // MessageBox.Show("Finished searching the document", "TagStyling", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (nStartPostion <= 0)
            {
                Btn_Replace.Enabled = false;
                //richTextBox1.ScrollToCaret();
                //richTextBox1.Focus();
                if (Combo_Option.SelectedIndex == 0)
                {
                    int d = 0;
                    nStartPostion = 0;
                    d = richTextBox1.Find(text, d, nOmitTagEndPostion, oFindCaseFnd);
                    if (d > 0)
                    {
                        if (Repallflag == false)
                        Find();
                    }
                }
                else if (Combo_Option.SelectedIndex == 2)
                {
                    nStartPostion = stapoint - nEndPostion;
                    if (Repallflag == false)
                    Find();
                }
            }
            
        }


private void txtBox_Fnd_KeyUp(object sender, KeyEventArgs e)
        {
            if (Checkbox_Wildfnd.Checked == true)
            {
                if (Tab_FndRep.SelectedIndex == 0)
                {
                    text = TxtBox_Fnd.Text;
                    if (text != "")
                    {
                        Btn_Find.Enabled = true;
                        Btn_Previous.Enabled = true;
                        Btn_REpFndWhat.Enabled = false;
                    }
                    else
                    {
                        Btn_Find.Enabled = false;
                        Btn_Previous.Enabled = false;
                        Checkbox_RepHighlight.Checked = false;

                    }
                }
                else if (Tab_FndRep.SelectedIndex == 1)
                {
                    text = TxtBox_RepFnd.Text;
                    if (text != "")
                    {
                        Btn_REpFndWhat.Enabled = true;
                        Btn_RepPrevious.Enabled = true;
                        Btn_Find.Enabled = false;

                    }
                    else
                    {
                        Btn_REpFndWhat.Enabled = false;
                        Btn_RepPrevious.Enabled = false;
                        Btn_Replace.Enabled = false;
                        btn_Replaceall.Enabled = false;
                        Checkbox_Highlight.Checked = false;
                        Checkbox_RepHighlight.Checked = false;
                        Checkbox_RepHighlight.Enabled = false;

                    }
                }
                return;
            }
                

            if (Tab_FndRep.SelectedIndex == 0)
            {
                text = TxtBox_Fnd.Text;
                if (text != "")
                {
                    Btn_Find.Enabled = true;
                    Btn_Previous.Enabled = true;
                    Btn_REpFndWhat.Enabled = false;
                }
                else
                {
                    Btn_Find.Enabled = false;
                    Btn_Previous.Enabled = false;
                    Checkbox_RepHighlight.Checked = false;
                    
                }
            }
            else if (Tab_FndRep.SelectedIndex == 1)
            {
                text = TxtBox_RepFnd.Text;
                if (text != "")
                {
                    Btn_REpFndWhat.Enabled = true;
                    Btn_RepPrevious.Enabled = true;
                    Btn_Find.Enabled = false;
                    
                }
                else
                {
                    Btn_REpFndWhat.Enabled = false;
                    Btn_RepPrevious.Enabled = false;
                    Btn_Replace.Enabled = false;
                    btn_Replaceall.Enabled = false;
                    Checkbox_Highlight.Checked = false;
                    Checkbox_RepHighlight.Checked = false;
                    Checkbox_RepHighlight.Enabled = false;
 
                }
            }

            

            findflag = false;
            if (text.Length >= 5)
            {
                Checkbox_Highlight.Visible = true;
                Checkbox_RepHighlight.Visible = true;

            }
            else
            {
                Checkbox_Highlight.Visible = false;
                Checkbox_RepHighlight.Visible = false;
            }

            if (e.KeyValue == '\r')
            {
                findflag = true;
               if (TxtBox_Fnd.Text != "")
                {
                    Btn_Find.Enabled = true;
                    if (!((e.KeyValue >= 112 && e.KeyValue <= 123) || (e.KeyValue >= 0 && e.KeyValue <= 20) || e.KeyValue == 27 || (e.KeyValue >= 32 && e.KeyValue <= 39) || (e.KeyValue >= 44 && e.KeyValue <= 46) || e.KeyValue == 40 || e.KeyValue == 144 || e.KeyValue == 145 || e.KeyValue == 93))
                    {
                        Find();
                    }
                    //Btn_Find.Focus();
                }
            }
            if (!((e.KeyValue >= 112 && e.KeyValue <= 123) || (e.KeyValue >= 0 && e.KeyValue <= 20) || e.KeyValue == 27 || (e.KeyValue >= 32 && e.KeyValue <= 39) || (e.KeyValue >= 44 && e.KeyValue <= 46) || e.KeyValue == 40 || e.KeyValue == 144 || e.KeyValue == 145 || e.KeyValue == 93))
            {
                //if (nStartPostion > 0)
                //    nStartPostion -= nEndPostion;
                if (text != "")
                {

                    Btn_Find.Enabled = true;
                    Find();
                    
                }
            }
            else if (text!= "")
            {
                //if (nStartPostion > 0)
                //    nStartPostion -= nEndPostion;
                Btn_Find.Enabled = true;

                Find();
                
            }
            else if (text == "")
            {
                Btn_Find.Enabled = false;
                Btn_Replace.Enabled = false;
                btn_Replaceall.Enabled = false;
                richTextBox1.Select(0, 0);
                richTextBox1.ScrollToCaret();
                            
            }


        }

private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //e.Handled = true;
            if (e.KeyChar == 26)
            {
                Undo_Click(sender, e);
            }
            else if (e.KeyChar == 25)
                Redo_Click(sender, e);
            else
            {
                if (e.KeyChar == 13)
                {
                    if (panflag == false)
                        return;
                    else
                    {
                        //if(e.KeyChar == 13
                        btn_Find_Click(sender, e);
                    }

                }
                else
                {
                    if (panflag == true)
                    {

                        if (e.KeyChar == 8)
                        {
                            if (text != "")
                            {
                                text = text.Substring(0, text.Length - 1);
                                if (Tab_FndRep.SelectedIndex == 0)
                                {
                                    TxtBox_Fnd.Text = text;
                                    TxtBox_Fnd.Focus();
                                }
                                else if (Tab_FndRep.SelectedIndex == 1)
                                {
                                    TxtBox_RepFnd.Text = text;
                                    TxtBox_RepFnd.Focus();
                                }

                            }
                            else
                            {
                                richTextBox1.Select(0, 0);
                                richTextBox1.ScrollToCaret();
                            }

                        }
                        else if (e.KeyChar == 27)
                        {
                            if (panel1.Visible == true)
                            {
                                btn_FndRepclose_Click(sender, e);
                                richTextBox1.Select(0, 0);
                                richTextBox1.ScrollToCaret();
                            }
                        }
                        else if (e.KeyChar == 24)
                        {
                            text = TxtBox_Fnd.Text = "";
                        }
                        else if (Tab_FndRep.SelectedIndex == 0)
                            text = TxtBox_Fnd.Text = TxtBox_Fnd.Text + e.KeyChar.ToString();
                        else if (Tab_FndRep.SelectedIndex == 1)
                            text = TxtBox_RepFnd.Text = TxtBox_RepFnd.Text + e.KeyChar.ToString();



                        //findflag = true;
                        //if
                        Find();
                        e.Handled = true;

                    }
                    else
                        saveflag = true;

                }
                if (TxtBox_Fnd.Text.Length >= 5)
                {
                    Checkbox_Highlight.Visible = true;
                    Checkbox_RepHighlight.Visible = true;

                }
                else
                {
                    Checkbox_Highlight.Visible = false;
                    Checkbox_RepHighlight.Visible = false;
                }
            }
        }

private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
        {
            panflag = false;
         
        }
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (panflag == true)
                e.Handled = true;
            
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.