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 and hidden Text

RichTextBox and hidden Text

Scheduled Pinned Locked Moved C#
helptutorialquestion
10 Posts 2 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.
  • B Offline
    B Offline
    Beringer
    wrote on last edited by
    #1

    I have come across an issue using the RichTextBox control and using hidden text. I have programatically placed hidden text in the control and then attempt to search for that text using the Find method. The indexes it returns are correct. However, when I call Select (or even set the SelectionStart and SelectionLength properties) to select some text that includes the hidden text this fails to work properly. The actual behavior is to reverse SelectionStart and SelectionLength. For example: this.textBox.SelectionStart = 45; this.textBox.SelectionLength = 0; -or- this.textBox.Selection(45, 0); Now look at the property values in the debuger and you will see SelectionStart = 0 and SelectionLength = 45. If I remove hidden text from the control everything works fine. Has anybody experienced this behavior? Just to be sure I wasn't messing with the values somewhere else, I commented out all other Selection calls and setting of the properties from my code and still had the issue. Thanks Eric

    B 1 Reply Last reply
    0
    • B Beringer

      I have come across an issue using the RichTextBox control and using hidden text. I have programatically placed hidden text in the control and then attempt to search for that text using the Find method. The indexes it returns are correct. However, when I call Select (or even set the SelectionStart and SelectionLength properties) to select some text that includes the hidden text this fails to work properly. The actual behavior is to reverse SelectionStart and SelectionLength. For example: this.textBox.SelectionStart = 45; this.textBox.SelectionLength = 0; -or- this.textBox.Selection(45, 0); Now look at the property values in the debuger and you will see SelectionStart = 0 and SelectionLength = 45. If I remove hidden text from the control everything works fine. Has anybody experienced this behavior? Just to be sure I wasn't messing with the values somewhere else, I commented out all other Selection calls and setting of the properties from my code and still had the issue. Thanks Eric

      B Offline
      B Offline
      Beringer
      wrote on last edited by
      #2

      I have investigated further and noted the following behavior: If I set the SelectionStart property to 45 it will stay at 45. However, when I set the SelectionLength property to say 0; then the problem arises. So it seems you can set one of the properties and it will store properly but when you set the second one the "switch" occurs. Thanks, Eric

      B 1 Reply Last reply
      0
      • B Beringer

        I have investigated further and noted the following behavior: If I set the SelectionStart property to 45 it will stay at 45. However, when I set the SelectionLength property to say 0; then the problem arises. So it seems you can set one of the properties and it will store properly but when you set the second one the "switch" occurs. Thanks, Eric

        B Offline
        B Offline
        Beringer
        wrote on last edited by
        #3

        Even more strange behavior: It seems that the Select method and setting of SelectionStart/SelectionLength will result in: SelectionStart = SelectionStart + SelectionLength and SelectionLength = 0 This appears to be the case no matter what the values are and the settings will occur after both properties have been set. Thanks, Eric

        M 1 Reply Last reply
        0
        • B Beringer

          Even more strange behavior: It seems that the Select method and setting of SelectionStart/SelectionLength will result in: SelectionStart = SelectionStart + SelectionLength and SelectionLength = 0 This appears to be the case no matter what the values are and the settings will occur after both properties have been set. Thanks, Eric

          M Offline
          M Offline
          mav northwind
          wrote on last edited by
          #4

          Hi! I have been working with hidden text in RichTextBoxes some time ago and couldn't find suspicious behaviour. Since you didn't write how you insert hidden text and didn't give the exact RTF string, all I can say is that selection seemed to be consistent if you are aware that a RTB's Text property will contain the hidden text (although the RTB doesn't show it) and the values used for selection are relative to the contents of your Text property. Regards, mav

          B 1 Reply Last reply
          0
          • M mav northwind

            Hi! I have been working with hidden text in RichTextBoxes some time ago and couldn't find suspicious behaviour. Since you didn't write how you insert hidden text and didn't give the exact RTF string, all I can say is that selection seemed to be consistent if you are aware that a RTB's Text property will contain the hidden text (although the RTB doesn't show it) and the values used for selection are relative to the contents of your Text property. Regards, mav

            B Offline
            B Offline
            Beringer
            wrote on last edited by
            #5

            I insert the Rtf string as follows at the current position of selection. textBox.SelectedRtf = "{\rtf1\ansi\v " + text + "\v0}" I also am aware that the Text property will have the hidden text in it. The Find method returns the correct index. The problem occurs when I try to move the selection to the location returned by Find and set the length of selected text. Please let me know if the above insertion is not valid. Thanks for your help, Eric

            M 1 Reply Last reply
            0
            • B Beringer

              I insert the Rtf string as follows at the current position of selection. textBox.SelectedRtf = "{\rtf1\ansi\v " + text + "\v0}" I also am aware that the Text property will have the hidden text in it. The Find method returns the correct index. The problem occurs when I try to move the selection to the location returned by Find and set the length of selected text. Please let me know if the above insertion is not valid. Thanks for your help, Eric

              M Offline
              M Offline
              mav northwind
              wrote on last edited by
              #6

              I just tried it. Created a RTB and then wrote:

              richTextBox1.Text = "test";
              richTextBox1.Select(2,0); // cursor between 'e' and 's'
              string text = "hidden";
              richTextBox1.SelectedRtf = @"{\rtf1\ansi\v " + text + @"\v0}";
              richTextBox1.Select(0,0);

              The RTB showed just "test", the cursor was at the beginning of the line, as expected. Then I tried to select something via code:

              richTextBox1.Select(1,8);

              and, like I expected, the RTB selected "es" inside "test". That's what I meant in my previous posting: richTextBox1.Text is "tehiddenst" and selection has to occurr according to this string. In my sample the selection goes from the first 'e' (index 1) 8 characters to the right ('ehiddens'). Since the hidden part isn't shown, only 2 characters are shown as selected, even though 8 characters actually are selected. Regards, mav

              B 2 Replies Last reply
              0
              • M mav northwind

                I just tried it. Created a RTB and then wrote:

                richTextBox1.Text = "test";
                richTextBox1.Select(2,0); // cursor between 'e' and 's'
                string text = "hidden";
                richTextBox1.SelectedRtf = @"{\rtf1\ansi\v " + text + @"\v0}";
                richTextBox1.Select(0,0);

                The RTB showed just "test", the cursor was at the beginning of the line, as expected. Then I tried to select something via code:

                richTextBox1.Select(1,8);

                and, like I expected, the RTB selected "es" inside "test". That's what I meant in my previous posting: richTextBox1.Text is "tehiddenst" and selection has to occurr according to this string. In my sample the selection goes from the first 'e' (index 1) 8 characters to the right ('ehiddens'). Since the hidden part isn't shown, only 2 characters are shown as selected, even though 8 characters actually are selected. Regards, mav

                B Offline
                B Offline
                Beringer
                wrote on last edited by
                #7

                Thanks for clearing this up. I won't be able to try this out until later. I'll let you know if I'm successful. Thanks Eric

                1 Reply Last reply
                0
                • M mav northwind

                  I just tried it. Created a RTB and then wrote:

                  richTextBox1.Text = "test";
                  richTextBox1.Select(2,0); // cursor between 'e' and 's'
                  string text = "hidden";
                  richTextBox1.SelectedRtf = @"{\rtf1\ansi\v " + text + @"\v0}";
                  richTextBox1.Select(0,0);

                  The RTB showed just "test", the cursor was at the beginning of the line, as expected. Then I tried to select something via code:

                  richTextBox1.Select(1,8);

                  and, like I expected, the RTB selected "es" inside "test". That's what I meant in my previous posting: richTextBox1.Text is "tehiddenst" and selection has to occurr according to this string. In my sample the selection goes from the first 'e' (index 1) 8 characters to the right ('ehiddens'). Since the hidden part isn't shown, only 2 characters are shown as selected, even though 8 characters actually are selected. Regards, mav

                  B Offline
                  B Offline
                  Beringer
                  wrote on last edited by
                  #8

                  Ok, thanks again for the input. I have looked at what is going on and still believe I'm doing things right (probably wrong since it doesn't work, but oh well!). I created a little test app to do what you did and was able to create the issue I'm having. If you don't mind please try: Create an form with richTextBox1 and button1 objects. Use the code below: private void button1_Click(object sender, EventArgs e) { int index = this.richTextBox1.Find("hidden2", RichTextBoxFinds.MatchCase | RichTextBoxFinds.NoHighlight); this.richTextBox1.Select(0, index); int temp = this.richTextBox1.SelectionStart; int temp2 = this.richTextBox1.SelectionLength; this.richTextBox1.SelectedText = "Eric"; } private void Form1_Load(object sender, EventArgs e) { this.richTextBox1.SelectedRtf = @"{\rtf1\ansi te\v hidden1\v0st1\par}"; this.richTextBox1.SelectedRtf = @"{\rtf1\ansi te\v hidden2\v0st2\par}"; //this.richTextBox1.SelectedRtf = @"{\rtf1\ansi\v hidden1\v0test1\par}"; //this.richTextBox1.SelectedRtf = @"{\rtf1\ansi\v hidden2\v0test2\par}"; } Now if you run the app with the first two lines of SelectedRtf statements (basically like your example) and run the app the selection process in the button click method works well; the resulting text in the RTB is: "Ericst2". Now if you use the other two lines of SelectedRtf statemsnt (basically what I'm doing) you will find the Select doesn't work. And behaves like I mentioned and the RTB shows: "test1Erictest2." Note the SelectionStart and SelectionLength properties don't match what was sent to Select. Thanks, Eric

                  M 1 Reply Last reply
                  0
                  • B Beringer

                    Ok, thanks again for the input. I have looked at what is going on and still believe I'm doing things right (probably wrong since it doesn't work, but oh well!). I created a little test app to do what you did and was able to create the issue I'm having. If you don't mind please try: Create an form with richTextBox1 and button1 objects. Use the code below: private void button1_Click(object sender, EventArgs e) { int index = this.richTextBox1.Find("hidden2", RichTextBoxFinds.MatchCase | RichTextBoxFinds.NoHighlight); this.richTextBox1.Select(0, index); int temp = this.richTextBox1.SelectionStart; int temp2 = this.richTextBox1.SelectionLength; this.richTextBox1.SelectedText = "Eric"; } private void Form1_Load(object sender, EventArgs e) { this.richTextBox1.SelectedRtf = @"{\rtf1\ansi te\v hidden1\v0st1\par}"; this.richTextBox1.SelectedRtf = @"{\rtf1\ansi te\v hidden2\v0st2\par}"; //this.richTextBox1.SelectedRtf = @"{\rtf1\ansi\v hidden1\v0test1\par}"; //this.richTextBox1.SelectedRtf = @"{\rtf1\ansi\v hidden2\v0test2\par}"; } Now if you run the app with the first two lines of SelectedRtf statements (basically like your example) and run the app the selection process in the button click method works well; the resulting text in the RTB is: "Ericst2". Now if you use the other two lines of SelectedRtf statemsnt (basically what I'm doing) you will find the Select doesn't work. And behaves like I mentioned and the RTB shows: "test1Erictest2." Note the SelectionStart and SelectionLength properties don't match what was sent to Select. Thanks, Eric

                    M Offline
                    M Offline
                    mav northwind
                    wrote on last edited by
                    #9

                    Ok, now I think I see what the problem is. The RTB seems to have a problem when the RTF starts with hidden text. I doubt that the behaviour is created by the .NET wrapper around the underlying rich edit control, but you can try to reproduce the same effect with an MFC program, for example, to verify this. Then you should try to contact Microsoft and let them know that there's a problem with the rich edit control, they might even be able to fix it easily and produce a hotfix. Some time ago I had a different problem with the RTB (the undo/redo buffer was cleared when you queried its Text property) and received a new version of the rich edit control that didn't show the behaviour. Altogether, I'm very content with the performance of Microsoft's support, so you should give it a try. Regards, mav

                    B 1 Reply Last reply
                    0
                    • M mav northwind

                      Ok, now I think I see what the problem is. The RTB seems to have a problem when the RTF starts with hidden text. I doubt that the behaviour is created by the .NET wrapper around the underlying rich edit control, but you can try to reproduce the same effect with an MFC program, for example, to verify this. Then you should try to contact Microsoft and let them know that there's a problem with the rich edit control, they might even be able to fix it easily and produce a hotfix. Some time ago I had a different problem with the RTB (the undo/redo buffer was cleared when you queried its Text property) and received a new version of the rich edit control that didn't show the behaviour. Altogether, I'm very content with the performance of Microsoft's support, so you should give it a try. Regards, mav

                      B Offline
                      B Offline
                      Beringer
                      wrote on last edited by
                      #10

                      I don't have the ability to make an MFC program, but I will definately let Microsoft support know. Thanks for your help. At least now I don't feel stupid! Eric

                      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