471,357 Members | 1,237 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

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.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!
Jun 29 '08 #1
6 7829
On Sat, 28 Jun 2008 21:23:46 -0700, Adam Sandler <co****@excite.comwrote:
[...]
In the source code, the returns are put in with "\n". I tried
replacing that with Environment.NewLine 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.NewLine, and using
Environment.NewLine would generally fix the problem.

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

Pete
Jun 29 '08 #2
On Jun 28, 10:37*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.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(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.
Jun 29 '08 #3
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.

"Adam Sandler" <co****@excite.comwrote in message
news:7e**********************************@w4g2000p rd.googlegroups.com...
On Jun 28, 10:37 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.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(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.

Jun 30 '08 #4
On Jun 29, 7:15*pm, "Family Tree Mike"
<FamilyTreeM...@ThisOldHouse.comwrote:
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?
Jun 30 '08 #5
On Sun, 29 Jun 2008 21:44:12 -0700, Adam Sandler <co****@excite.comwrote:
On Jun 29, 7:15*pm, "Family Tree Mike"
<FamilyTreeM...@ThisOldHouse.comwrote:
>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?
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
Jun 30 '08 #6
I believe that my command to set the richtextbox was (for example):

RichTextBox1.Text = StringBuilder1.ToString();

"Adam Sandler" <co****@excite.comwrote in message
news:75**********************************@u36g2000 prf.googlegroups.com...
On Jun 29, 7:15 pm, "Family Tree Mike"
<FamilyTreeM...@ThisOldHouse.comwrote:
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?

Jun 30 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Alfred Tascon | last post: by
7 posts views Thread by Newbie | last post: by
3 posts views Thread by =?Utf-8?B?cm9kY2hhcg==?= | last post: by
1 post views Thread by =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.