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. Other Discussions
  3. The Weird and The Wonderful
  4. Rtf -WTF: why is it different?

Rtf -WTF: why is it different?

Scheduled Pinned Locked Moved The Weird and The Wonderful
debuggingquestion
6 Posts 4 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
    Bernhard Hiller
    wrote on last edited by
    #1

    A RichTextBox has an Rtf property, and a SelectedRtf 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);

    :^)

    L R L 3 Replies Last reply
    0
    • B Bernhard Hiller

      A RichTextBox has an Rtf property, and a SelectedRtf 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);

      :^)

      L Offline
      L Offline
      ledtech3
      wrote on last edited by
      #2

      This might explain it. http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.rtf(v=vs.110).aspx[^]

      B 1 Reply Last reply
      0
      • L ledtech3

        This might explain it. http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.rtf(v=vs.110).aspx[^]

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        No, it does not. By the way, this is the Weird and Wonderful, or Coding 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: since Rtf is always longer than SelectedRtf, the whole is more than the sum of its parts...

        L 1 Reply Last reply
        0
        • B Bernhard Hiller

          A RichTextBox has an Rtf property, and a SelectedRtf 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);

          :^)

          R Offline
          R Offline
          Rob Grainger
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • B Bernhard Hiller

            No, it does not. By the way, this is the Weird and Wonderful, or Coding 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: since Rtf is always longer than SelectedRtf, the whole is more than the sum of its parts...

            L Offline
            L Offline
            ledtech3
            wrote on last edited by
            #5

            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 Integer

            Dim 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 Integer

            Dim 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.

            1 Reply Last reply
            0
            • B Bernhard Hiller

              A RichTextBox has an Rtf property, and a SelectedRtf 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);

              :^)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Agree. :)

              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