Rtf -WTF: why is it different?
-
A
RichTextBox
has anRtf
property, and aSelectedRtf
property. Even when all text is selected, their values differ. Just try:string rtf1 = richTextBox1.Rtf;
richTextBox1.SelectAll();
string rtf2 = richTextBox1.SelectedRtf;
System.Diagnostics.Debug.Assert(rtf1 == rtf2);:^)
-
A
RichTextBox
has anRtf
property, and aSelectedRtf
property. Even when all text is selected, their values differ. Just try:string rtf1 = richTextBox1.Rtf;
richTextBox1.SelectAll();
string rtf2 = richTextBox1.SelectedRtf;
System.Diagnostics.Debug.Assert(rtf1 == rtf2);:^)
-
This might explain it. http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.rtf(v=vs.110).aspx[^]
No, it does not. By the way, this is the
Weird and Wonderful
, orCoding Horrors
forum - I really intended to place it here, because it is quite a WTF, and I do not expect a sensible answer for it (OK, if you know it, let me here it). It rather looks like a philisophical design decision: sinceRtf
is always longer thanSelectedRtf
, the whole is more than the sum of its parts... -
A
RichTextBox
has anRtf
property, and aSelectedRtf
property. Even when all text is selected, their values differ. Just try:string rtf1 = richTextBox1.Rtf;
richTextBox1.SelectAll();
string rtf2 = richTextBox1.SelectedRtf;
System.Diagnostics.Debug.Assert(rtf1 == rtf2);:^)
I'm not certain, but suspect it is due to the fact that an RTF document contains various sections in addition to the formatting codes needed. Here's one version of the spec: RTF 1.7[^] (not the latest, but most should be similar). A bit like copying HTML to clipboard - if just the markup is copied, other information supplied in the header section may be omitted, so styles etc. wouldn't work correctly. The SelectedText may contain just enough of these for the selection, while the Rtf property will, presumably, include all those in the entire RTF "document" including standard "document level" header sections (a bit like the <head> section in HTML).
"If you don't fail at least 90 percent of the time, you're not aiming high enough." Alan Kay.
-
No, it does not. By the way, this is the
Weird and Wonderful
, orCoding Horrors
forum - I really intended to place it here, because it is quite a WTF, and I do not expect a sensible answer for it (OK, if you know it, let me here it). It rather looks like a philisophical design decision: sinceRtf
is always longer thanSelectedRtf
, the whole is more than the sum of its parts...Try this test to get a better Idea. Create a test project with a richtextbox and a normal text box. two buttons and a label to show the length In VB.net it would look like.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim selLen As IntegerDim selectedRtf As String = RichTextBox1.SelectedRtf selLen = selectedRtf.Length Label1.Text = selLen.ToString TextBox1.Text = selectedRtf
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim selLen As IntegerDim selectedRtf As String = RichTextBox1.Rtf selLen = selectedRtf.Length Label1.Text = selLen.ToString TextBox1.Text = selectedRtf
End Sub
Using a converter to convert to C# it would look like this.
private void Button1_Click(System.Object sender, System.EventArgs e)
{
int selLen = 0;string selectedRtf = RichTextBox1.SelectedRtf; selLen = selectedRtf.Length; Label1.Text = selLen.ToString; TextBox1.Text = selectedRtf;
}
private void Button2_Click(System.Object sender, System.EventArgs e)
{
int selLen = 0;string selectedRtf = RichTextBox1.Rtf; selLen = selectedRtf.Length; Label1.Text = selLen.ToString; TextBox1.Text = selectedRtf;
}
Next create some text in wordpad with different formating. Copy paste to the richtextbox (ctl+V) what this code does is gets the length of the string, the entire string with the hidden formating and copies it to the plain textbox to view all of it. You can add the line to select all or just select it on the RTF box. After going back and forth between the buttons you may notice that the second one shows more formatting and thus it is longer.
-
A
RichTextBox
has anRtf
property, and aSelectedRtf
property. Even when all text is selected, their values differ. Just try:string rtf1 = richTextBox1.Rtf;
richTextBox1.SelectAll();
string rtf2 = richTextBox1.SelectedRtf;
System.Diagnostics.Debug.Assert(rtf1 == rtf2);:^)