RichTextBox and hidden Text
-
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
-
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
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
-
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
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
-
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
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 yourText
property. Regards, mav -
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 yourText
property. Regards, mavI 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
-
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
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 -
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 -
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, mavOk, 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 -
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, EricOk, 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 -
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