problem with Clipboard.SetText

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Sandler

    problem with Clipboard.SetText

    Hello,

    I have a richtextbox and a method which copies the contents of the
    richtextbox to the clipboard -- like so:

    Clipboard.SetTe xt(this.richTex tBox1.Text);

    The problem is if there are any returns, and the user pastes the
    clipboard contents into notepad, all the data appears on one line --
    with a bunch of non printing characters (which show up as boxes) where
    the returns should be. It works okay in wordpad though

    So the desired paste operation in notepad yields this:

    ! 6/28/2008 10:15:58 PM netadmin<<box>> ! Enable multicast
    routing.<<box>> ip multicast-routing distributed<<bo x>>ip multicast
    route-limit 1000

    When I want it to yield this:

    ! 6/28/2008 10:15:58 PM netadmin

    ! Enable multicast routing.

    ip multicast-routing distributed
    ip multicast route-limit 1000

    In the source code, the returns are put in with "\n". I tried
    replacing that with Environment.New Line and that doesn't help.

    So, how can I get the contents of the clipboard copy to paste into a
    text editor where returns aren't treated as special characters and the
    text is formatted vertically instead of appearing all on one line?

    Suggestions are greatly appreciated.

    Thanks!
  • Peter Duniho

    #2
    Re: problem with Clipboard.SetTe xt

    On Sat, 28 Jun 2008 21:23:46 -0700, Adam Sandler <corn29@excite. comwrote:
    [...]
    In the source code, the returns are put in with "\n". I tried
    replacing that with Environment.New Line and that doesn't help.
    Are you sure? The problem you're describing sounds exactly like what
    would happen if you didn't use Environment.New Line, and using
    Environment.New Line would generally fix the problem.

    What happens if you try "\r\n" explicitly instead of "\n"?

    Pete

    Comment

    • Adam Sandler

      #3
      Re: problem with Clipboard.SetTe xt

      On Jun 28, 10:37 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
      wrote:
      Are you sure?
      Yes I'm sure... I'd attach a screen shot if I could showing you the
      non printing characters which show up using notepad.

      In the meantime, in a new project, I threw a richtextbox and a button
      on a form -- try this code out for yourself:

      private void button1_Click(o bject sender, EventArgs e)
      {
      richTextBox1.Ap pendText("test" );
      richTextBox1.Ap pendText(Enviro nment.NewLine);
      richTextBox1.Ap pendText("to");
      richTextBox1.Ap pendText(Enviro nment.NewLine);
      richTextBox1.Ap pendText("copy" );
      richTextBox1.Ap pendText(Enviro nment.NewLine);
      richTextBox1.Ap pendText("clipb oard");

      Clipboard.SetTe xt(richTextBox1 .Text);
      }

      Now, open notepad and paste... the output is all on a single line.
      Where the returns should be there are the same non-printing characters
      I mentioned in the original post.

      Comment

      • Family Tree Mike

        #4
        Re: problem with Clipboard.SetTe xt

        I am seeing the same behaviour that you are in Notepad, but not Notepad++, a
        freeware tool I prefer. When I change your code to use a string builder to
        build up the string, then put that into the clipboard, all goes well in
        Notepad. I suspect "somehow" what you get back from RichTextBox1.Te xt is
        not what you put into it.

        "Adam Sandler" <corn29@excite. comwrote in message
        news:7e895e08-58ca-4c09-bfc1-7778876ace5f@w4 g2000prd.google groups.com...
        On Jun 28, 10:37 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
        wrote:
        Are you sure?
        Yes I'm sure... I'd attach a screen shot if I could showing you the
        non printing characters which show up using notepad.

        In the meantime, in a new project, I threw a richtextbox and a button
        on a form -- try this code out for yourself:

        private void button1_Click(o bject sender, EventArgs e)
        {
        richTextBox1.Ap pendText("test" );
        richTextBox1.Ap pendText(Enviro nment.NewLine);
        richTextBox1.Ap pendText("to");
        richTextBox1.Ap pendText(Enviro nment.NewLine);
        richTextBox1.Ap pendText("copy" );
        richTextBox1.Ap pendText(Enviro nment.NewLine);
        richTextBox1.Ap pendText("clipb oard");

        Clipboard.SetTe xt(richTextBox1 .Text);
        }

        Now, open notepad and paste... the output is all on a single line.
        Where the returns should be there are the same non-printing characters
        I mentioned in the original post.

        Comment

        • Adam Sandler

          #5
          Re: problem with Clipboard.SetTe xt

          On Jun 29, 7:15 pm, "Family Tree Mike"
          <FamilyTreeM... @ThisOldHouse.c omwrote:
          I am seeing the same behaviour that you are in Notepad, but not Notepad++, a
          freeware tool I prefer.  When I change your code to use a string builder to
          build up the string, then put that into the clipboard, all goes well in
          Notepad.  I suspect "somehow" what you get back from RichTextBox1.Te xt is
          not what you put into it.
          Not everyone has Notepad++ and I have to account for that.

          The code I posted was an example -- NOT what is in the actual
          application. I too am using a stringbuilder in the app. But I could
          have sworn that when I tried to use richTextBox1.Ap pendText(), it
          threw an error when trying to use a stringbuilder as a parameter. So
          my code converts the stringbuilder to a string and then sends it to
          AppendText.

          How did you put a stringbuilder directly in a richtextbox?

          Comment

          • Peter Duniho

            #6
            Re: problem with Clipboard.SetTe xt

            On Sun, 29 Jun 2008 21:44:12 -0700, Adam Sandler <corn29@excite. comwrote:
            On Jun 29, 7:15 pm, "Family Tree Mike"
            <FamilyTreeM... @ThisOldHouse.c omwrote:
            >I am seeing the same behaviour that you are in Notepad, but not
            >Notepad++, a
            >freeware tool I prefer.  When I change your code to use a string
            >builder to
            >build up the string, then put that into the clipboard, all goes well in
            >Notepad.  I suspect "somehow" what you get back from RichTextBox1.Te xt
            >is
            >not what you put into it.
            >
            Not everyone has Notepad++ and I have to account for that.
            >
            The code I posted was an example -- NOT what is in the actual
            application. I too am using a stringbuilder in the app. But I could
            have sworn that when I tried to use richTextBox1.Ap pendText(), it
            threw an error when trying to use a stringbuilder as a parameter. So
            my code converts the stringbuilder to a string and then sends it to
            AppendText.
            >
            How did you put a stringbuilder directly in a richtextbox?
            As he said, he just uses StringBuilder to create the string, and then puts
            that result into the clipboard. No RTB involved.

            As far as the actual problem goes, I really don't know why the RTB control
            would insist on using just plain newline characters ('\n') for its newline
            delimiter, but it seems pretty clear that's what's going on. I have some
            vague memory of this issue from the past, actually, but don't recall any
            details. You might want to Google this newsgroup to see if there was a
            previous discussion about the RTB and newlines, and whether there's any
            useful advice in there.

            Barring that, it seems like the RTB is indeed explicitly changing any
            newlines to just use the newline character, and if you don't want to
            simply live with that (as you've seen, pretty much any of the more
            advanced programs, like Notepad++, WordPad, etc. will handle them
            fine...it is, after all, the convention used pretty much everywhere
            _except_ Windows), you will probably have to do a search & replace before
            pasting the code to the clipboard.

            Pete

            Comment

            • Family Tree Mike

              #7
              Re: problem with Clipboard.SetTe xt

              I believe that my command to set the richtextbox was (for example):

              RichTextBox1.Te xt = StringBuilder1. ToString();

              "Adam Sandler" <corn29@excite. comwrote in message
              news:75fbfb14-70c4-4066-b08f-c7585aafe6fd@u3 6g2000prf.googl egroups.com...
              On Jun 29, 7:15 pm, "Family Tree Mike"
              <FamilyTreeM... @ThisOldHouse.c omwrote:
              I am seeing the same behaviour that you are in Notepad, but not Notepad++,
              a
              freeware tool I prefer. When I change your code to use a string builder to
              build up the string, then put that into the clipboard, all goes well in
              Notepad. I suspect "somehow" what you get back from RichTextBox1.Te xt is
              not what you put into it.
              Not everyone has Notepad++ and I have to account for that.

              The code I posted was an example -- NOT what is in the actual
              application. I too am using a stringbuilder in the app. But I could
              have sworn that when I tried to use richTextBox1.Ap pendText(), it
              threw an error when trying to use a stringbuilder as a parameter. So
              my code converts the stringbuilder to a string and then sends it to
              AppendText.

              How did you put a stringbuilder directly in a richtextbox?

              Comment

              Working...