473,389 Members | 1,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,389 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 8125
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: xyz | last post by:
I have a menu item to handle clipboard actions (cut, copy, paste). When I paste text that I copied from a Hungarian web page, the display looks normal on my RichTextBox, but the text retrieved from...
1
by: Alfred Tascon | last post by:
Who knows how to create a Clipboard object/entry that when pasted produces a URL link - a link that when pasted into Word can be ctrl-clicked to launch a browser? Any help appreciated. Oh and i...
0
by: Raymond Bissonnette | last post by:
Hi, Any idea what's wrong with these two lines? I get nothing on the clipboard Dim sHTML As String = "<p>...</p>" Clipboard.SetText(sHTML, TextDataFormat.Html) TextDataFormat.Text works...
0
by: renfes | last post by:
Hey everyone. I have a program that watches a .txt log file from a chat client or a game and based on each line, triggers events. One thing I am trying to do is send the contents of a text file to...
1
by: Sefner | last post by:
Hey everyone. I have a program that watches a .txt log file from a chat client or a game and based on each line, triggers events. One thing I am trying to do is send the contents of a text file to...
7
by: Newbie | last post by:
How do I clear the clipboard in VB.NET 2003? TIA Newbie
3
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i'm using the following in my console app: My.Computer.Clipboard.SetText(sb.ToString()) i'm getting inconsistent results with the copy. sometimes it works and most of the time i get...
1
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, why do i get this on Vista, while trying to set text to clipboard: Error: System.Runtime.InteropServices.ExternalException: Der angeforderte Clipboard-Vorgang war nicht erfolgreich. bei...
1
by: advsoft | last post by:
I'm trying to copy to the clipboard a text that is read from the serial port, but when try to use: Clipboard.SetText(MyTexo,MyText Format) give me the follow error: Current thread must be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.