Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. richTextBox problem 2

richTextBox problem 2

Scheduled Pinned Locked Moved C#
questionhelp
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Q12_
    wrote on last edited by
    #1

    This Code is working well but wrong. Wrong because it lower all text -because of the MatchCase method. How can I make it work without pre-changing anything inside (exception for color text to find)?

    int posTxt = 0;
    private void button2_Click(object sender, EventArgs e)
    {

            string searchText = textBox2.Text.ToLower();
            string alltxt = richTextBox1.Text.ToLower();
    
            if (alltxt.Contains(searchText))
            {
                richTextBox1.Text = alltxt;
                while (posTxt != -1)
                {
                    posTxt = richTextBox1.Find(searchText, posTxt, RichTextBoxFinds.MatchCase);
                    if (richTextBox1.SelectedText == searchText)
                    {
                        richTextBox1.SelectionLength = searchText.Length;
                        richTextBox1.SelectionColor = Color.Red;
                    }
                    posTxt = richTextBox1.Find(searchText, posTxt + 1, RichTextBoxFinds.MatchCase);
                }
            }
            posTxt = 0;
        }
    
    L B _ 3 Replies Last reply
    0
    • _ _Q12_

      This Code is working well but wrong. Wrong because it lower all text -because of the MatchCase method. How can I make it work without pre-changing anything inside (exception for color text to find)?

      int posTxt = 0;
      private void button2_Click(object sender, EventArgs e)
      {

              string searchText = textBox2.Text.ToLower();
              string alltxt = richTextBox1.Text.ToLower();
      
              if (alltxt.Contains(searchText))
              {
                  richTextBox1.Text = alltxt;
                  while (posTxt != -1)
                  {
                      posTxt = richTextBox1.Find(searchText, posTxt, RichTextBoxFinds.MatchCase);
                      if (richTextBox1.SelectedText == searchText)
                      {
                          richTextBox1.SelectionLength = searchText.Length;
                          richTextBox1.SelectionColor = Color.Red;
                      }
                      posTxt = richTextBox1.Find(searchText, posTxt + 1, RichTextBoxFinds.MatchCase);
                  }
              }
              posTxt = 0;
          }
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      You could use the lower-case versions of the search string and the text to locate the match (or matches); then remove whatever exists at the match position (you know position and length), and then insert the new text with its original casing. :)

      Luc Pattyn [My Articles] Nil Volentibus Arduum

      The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
      Please use <PRE> tags for code snippets, they improve readability.
      CP Vanity has been updated to V2.3

      _ 1 Reply Last reply
      0
      • L Luc Pattyn

        You could use the lower-case versions of the search string and the text to locate the match (or matches); then remove whatever exists at the match position (you know position and length), and then insert the new text with its original casing. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
        Please use <PRE> tags for code snippets, they improve readability.
        CP Vanity has been updated to V2.3

        _ Offline
        _ Offline
        _Q12_
        wrote on last edited by
        #3

        this was my original thought but I have problems putting in practice. The search string do not have a lower-case version. http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextboxfinds.aspx[^] I must unfortunately bring down all text ToLower. This is my problem-how to avoid that? Can you give me an example, please? thanks.

        L 1 Reply Last reply
        0
        • _ _Q12_

          this was my original thought but I have problems putting in practice. The search string do not have a lower-case version. http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextboxfinds.aspx[^] I must unfortunately bring down all text ToLower. This is my problem-how to avoid that? Can you give me an example, please? thanks.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          please read my reply again: you search using the lower-case versions, only to find the location. you modify using the actual text, not the lower-case one (the positions are the same for both!) :)

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
          Please use <PRE> tags for code snippets, they improve readability.
          CP Vanity has been updated to V2.3

          _ 1 Reply Last reply
          0
          • L Luc Pattyn

            please read my reply again: you search using the lower-case versions, only to find the location. you modify using the actual text, not the lower-case one (the positions are the same for both!) :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
            Please use <PRE> tags for code snippets, they improve readability.
            CP Vanity has been updated to V2.3

            _ Offline
            _ Offline
            _Q12_
            wrote on last edited by
            #5

            this is what i have done so far... but with the same result. The text in richTextBox1 is changing to lower and I dont see from where.

            int posTxt = 0;
            private void button2_Click(object sender, EventArgs e)
            {
            string searchText = textBox2.Text.ToLower();
            //string alltxt = richTextBox1.Text.ToLower();
            string alltxt = richTextBox1.Text.ToLower();
            RichTextBox rtb = new RichTextBox(); rtb = richTextBox1;

                    if (alltxt.Contains(searchText))
                    {
                        //richTextBox1.Text = alltxt;
                        rtb.Text = alltxt;
                        
                        while (posTxt != -1)
                        {
                            posTxt = rtb.Find(searchText, posTxt, RichTextBoxFinds.MatchCase);
                            if (rtb.SelectedText == searchText)
                            {
                                richTextBox1.SelectionLength = searchText.Length;
                                richTextBox1.SelectionColor = Color.Red;
                            }
                            posTxt = rtb.Find(searchText, posTxt + 1, RichTextBoxFinds.MatchCase);
                        }
                    }
                    posTxt = 0;
                }
            
            L _ 2 Replies Last reply
            0
            • _ _Q12_

              this is what i have done so far... but with the same result. The text in richTextBox1 is changing to lower and I dont see from where.

              int posTxt = 0;
              private void button2_Click(object sender, EventArgs e)
              {
              string searchText = textBox2.Text.ToLower();
              //string alltxt = richTextBox1.Text.ToLower();
              string alltxt = richTextBox1.Text.ToLower();
              RichTextBox rtb = new RichTextBox(); rtb = richTextBox1;

                      if (alltxt.Contains(searchText))
                      {
                          //richTextBox1.Text = alltxt;
                          rtb.Text = alltxt;
                          
                          while (posTxt != -1)
                          {
                              posTxt = rtb.Find(searchText, posTxt, RichTextBoxFinds.MatchCase);
                              if (rtb.SelectedText == searchText)
                              {
                                  richTextBox1.SelectionLength = searchText.Length;
                                  richTextBox1.SelectionColor = Color.Red;
                              }
                              posTxt = rtb.Find(searchText, posTxt + 1, RichTextBoxFinds.MatchCase);
                          }
                      }
                      posTxt = 0;
                  }
              
              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              _q12_ wrote:

              The text in richTextBox1 is changing to lower and I dont see from where.

              _q12_ wrote:

              string alltxt = richTextBox1.Text.ToLower();

              _q12_ wrote:

              rtb.Text = alltxt;

              X|

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
              Please use <PRE> tags for code snippets, they improve readability.
              CP Vanity has been updated to V2.3

              _ 1 Reply Last reply
              0
              • _ _Q12_

                this is what i have done so far... but with the same result. The text in richTextBox1 is changing to lower and I dont see from where.

                int posTxt = 0;
                private void button2_Click(object sender, EventArgs e)
                {
                string searchText = textBox2.Text.ToLower();
                //string alltxt = richTextBox1.Text.ToLower();
                string alltxt = richTextBox1.Text.ToLower();
                RichTextBox rtb = new RichTextBox(); rtb = richTextBox1;

                        if (alltxt.Contains(searchText))
                        {
                            //richTextBox1.Text = alltxt;
                            rtb.Text = alltxt;
                            
                            while (posTxt != -1)
                            {
                                posTxt = rtb.Find(searchText, posTxt, RichTextBoxFinds.MatchCase);
                                if (rtb.SelectedText == searchText)
                                {
                                    richTextBox1.SelectionLength = searchText.Length;
                                    richTextBox1.SelectionColor = Color.Red;
                                }
                                posTxt = rtb.Find(searchText, posTxt + 1, RichTextBoxFinds.MatchCase);
                            }
                        }
                        posTxt = 0;
                    }
                
                _ Offline
                _ Offline
                _Erik_
                wrote on last edited by
                #7

                As Luc has already suggested: Do not assign alltxt to rtb.Text nor richTextBox1.Text; Actually, remove rtb variable, you don't need it; Use alltxt.IndexOf method instead of rtb.Find method to search for the text you want. The indexes will be the same. Use these indexes to make the changes on your rich text box control. The lines:

                RichTextBox rtb = new RichTextBox(); rtb = richTextBox1;

                are useless becouse RichTextBox is a reference type, so your local rtb variable and your richTextBox1 will both be pointing to the same object.

                _ 1 Reply Last reply
                0
                • _ _Q12_

                  This Code is working well but wrong. Wrong because it lower all text -because of the MatchCase method. How can I make it work without pre-changing anything inside (exception for color text to find)?

                  int posTxt = 0;
                  private void button2_Click(object sender, EventArgs e)
                  {

                          string searchText = textBox2.Text.ToLower();
                          string alltxt = richTextBox1.Text.ToLower();
                  
                          if (alltxt.Contains(searchText))
                          {
                              richTextBox1.Text = alltxt;
                              while (posTxt != -1)
                              {
                                  posTxt = richTextBox1.Find(searchText, posTxt, RichTextBoxFinds.MatchCase);
                                  if (richTextBox1.SelectedText == searchText)
                                  {
                                      richTextBox1.SelectionLength = searchText.Length;
                                      richTextBox1.SelectionColor = Color.Red;
                                  }
                                  posTxt = richTextBox1.Find(searchText, posTxt + 1, RichTextBoxFinds.MatchCase);
                              }
                          }
                          posTxt = 0;
                      }
                  
                  B Offline
                  B Offline
                  BobJanova
                  wrote on last edited by
                  #8

                  richTextBox1.Text = alltxt;

                  Why are you doing that? You are explicitly setting the text to the lower case version in that line. The only difference between the text property and alltxt is that you ran ToLower on alltxt! Also, RichTextBox.Find, if you want to use it, already searches case-insensitively by default. Did you read the documentation for RichTextBoxFinds[^]? Passing RichTextBoxFinds.None will cause the search to be case-insensitive – you explicitly asked for a case sensitive one, for some reason.

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    _q12_ wrote:

                    The text in richTextBox1 is changing to lower and I dont see from where.

                    _q12_ wrote:

                    string alltxt = richTextBox1.Text.ToLower();

                    _q12_ wrote:

                    rtb.Text = alltxt;

                    X|

                    Luc Pattyn [My Articles] Nil Volentibus Arduum

                    The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                    Please use <PRE> tags for code snippets, they improve readability.
                    CP Vanity has been updated to V2.3

                    _ Offline
                    _ Offline
                    _Q12_
                    wrote on last edited by
                    #9

                    yes... but... only rtb.Text = richTextBox1.Text; not inverse.

                    RichTextBox rtb = new RichTextBox(); rtb = richTextBox1;

                    meaning: richTextBox1.Text is not equal with rtb.Text. :confused: anyway... its not about the new rtb.

                    L D 2 Replies Last reply
                    0
                    • _ _Q12_

                      yes... but... only rtb.Text = richTextBox1.Text; not inverse.

                      RichTextBox rtb = new RichTextBox(); rtb = richTextBox1;

                      meaning: richTextBox1.Text is not equal with rtb.Text. :confused: anyway... its not about the new rtb.

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      _q12_ wrote:

                      rtb = richTextBox1;

                      WRONG! you have copied the reference, so both rtb and richTextBox1 point to the same object. the new RichTextBox you also created is dead right away, it is never used (it also is not needed!). BTW: Eric already told you all that. I suggest you choose, buy and thoroughly study an introductory book on C#. :)

                      Luc Pattyn [My Articles] Nil Volentibus Arduum

                      The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                      Please use <PRE> tags for code snippets, they improve readability.
                      CP Vanity has been updated to V2.3

                      1 Reply Last reply
                      0
                      • _ _Q12_

                        This Code is working well but wrong. Wrong because it lower all text -because of the MatchCase method. How can I make it work without pre-changing anything inside (exception for color text to find)?

                        int posTxt = 0;
                        private void button2_Click(object sender, EventArgs e)
                        {

                                string searchText = textBox2.Text.ToLower();
                                string alltxt = richTextBox1.Text.ToLower();
                        
                                if (alltxt.Contains(searchText))
                                {
                                    richTextBox1.Text = alltxt;
                                    while (posTxt != -1)
                                    {
                                        posTxt = richTextBox1.Find(searchText, posTxt, RichTextBoxFinds.MatchCase);
                                        if (richTextBox1.SelectedText == searchText)
                                        {
                                            richTextBox1.SelectionLength = searchText.Length;
                                            richTextBox1.SelectionColor = Color.Red;
                                        }
                                        posTxt = richTextBox1.Find(searchText, posTxt + 1, RichTextBoxFinds.MatchCase);
                                    }
                                }
                                posTxt = 0;
                            }
                        
                        _ Offline
                        _ Offline
                        _Q12_
                        wrote on last edited by
                        #11

                        Problems Solved: Sincerely, I did changed RichTextBoxFinds.None before posting here on forum, but for some strange reason, it did not work back then... Now it works fine. I have another problem...see it beneath code. Here is the functional code:

                        int posTxt = 0; string tempText = "";
                        private void button2_Click(object sender, EventArgs e)
                        {
                        richTextBox1.Text = tempText;//somewhere in the page load : tempText = richTextBox1.Text;

                                string searchText = textBox2.Text.ToLower();
                                string alltxt = richTextBox1.Text.ToLower();
                        
                                if (alltxt.Contains(searchText))
                                {
                                    //problems at start
                                    if (posTxt <= 0)
                                    {
                                        richTextBox1.Text = richTextBox1.Text.Insert(0, " "); 
                                        posTxt = 1;
                                    }
                                    //colorization at found text 
                                    while (posTxt > 0)
                                    {
                                        posTxt = richTextBox1.Find(searchText, posTxt, RichTextBoxFinds.None);
                                        if (richTextBox1.SelectedText.ToLower() == searchText)
                                        {
                                            richTextBox1.SelectionLength = searchText.Length;
                                            richTextBox1.SelectionColor = Color.Red;
                                        }
                                        posTxt = richTextBox1.Find(searchText, posTxt + 1, RichTextBoxFinds.None);
                                    }
                                }
                                posTxt = 0;
                            }
                        

                        But i have a little problem now... If I load a *.rtf file with formatting text and search a word in that richTextBox1 that show that file, the text is re-formatting, loosing its original format. How to maintain its original format, and select as usual(with red marking)?

                        1 Reply Last reply
                        0
                        • _ _Erik_

                          As Luc has already suggested: Do not assign alltxt to rtb.Text nor richTextBox1.Text; Actually, remove rtb variable, you don't need it; Use alltxt.IndexOf method instead of rtb.Find method to search for the text you want. The indexes will be the same. Use these indexes to make the changes on your rich text box control. The lines:

                          RichTextBox rtb = new RichTextBox(); rtb = richTextBox1;

                          are useless becouse RichTextBox is a reference type, so your local rtb variable and your richTextBox1 will both be pointing to the same object.

                          _ Offline
                          _ Offline
                          _Q12_
                          wrote on last edited by
                          #12

                          in some desperation I was using "RichTextBox rtb " variant, copying its locations to the real one... But I managed to find the solution after all. Thanks!

                          1 Reply Last reply
                          0
                          • _ _Q12_

                            yes... but... only rtb.Text = richTextBox1.Text; not inverse.

                            RichTextBox rtb = new RichTextBox(); rtb = richTextBox1;

                            meaning: richTextBox1.Text is not equal with rtb.Text. :confused: anyway... its not about the new rtb.

                            D Offline
                            D Offline
                            Dan Mos
                            wrote on last edited by
                            #13

                            As told earlier by Luc, rtb and richtextBox1 refer(point to the same object). Think of it this way: rtb and richtextbox1 are just some names that help you deal the the real thing. A guick fix for what you are trying to do with this line of code would be:

                            RichTextBox rtb = new RichTextBox(); rtb**.Text** = richTextBox1**.Text**;

                            Now rtb does not point to the same object but it just copied the text from richtexbox1. Clearer than that I don't know how to explain.

                            All the best, Dan

                            modified on Friday, May 27, 2011 3:35 PM

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups