problem with Clipboard.SetText

  • Thread starter Thread starter Adam Sandler
  • Start date Start date
A

Adam Sandler

Hello,

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

Clipboard.SetText(this.richTextBox1.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<<box>>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.NewLine 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!
 
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(object sender, EventArgs e)
{
richTextBox1.AppendText("test");
richTextBox1.AppendText(Environment.NewLine);
richTextBox1.AppendText("to");
richTextBox1.AppendText(Environment.NewLine);
richTextBox1.AppendText("copy");
richTextBox1.AppendText(Environment.NewLine);
richTextBox1.AppendText("clipboard");

Clipboard.SetText(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.
 
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.Text is
not what you put into it.

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(object sender, EventArgs e)
{
richTextBox1.AppendText("test");
richTextBox1.AppendText(Environment.NewLine);
richTextBox1.AppendText("to");
richTextBox1.AppendText(Environment.NewLine);
richTextBox1.AppendText("copy");
richTextBox1.AppendText(Environment.NewLine);
richTextBox1.AppendText("clipboard");

Clipboard.SetText(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.
 
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.Text 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.AppendText(), 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?
 
I believe that my command to set the richtextbox was (for example):

RichTextBox1.Text = StringBuilder1.ToString();

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.Text 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.AppendText(), 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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top