Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I'm currently implementing a text editor which needs to be able to handle non-breaking spaces .(  /  ).

My problem is that we need to copy&paste pre-existing MS Word content into the TextBox (or RichTextBox). However, the non-breaking space was lost somewhere between copying from Word and inserting it into my textbox.

Have any of you encountered this problem? If so, did you figure out what to do about it?
I'm thinking that maybe I'm overlooking something really obvious here ...


Anyway, the following is the code I used to look at the pasted text.

C#
private void PasteHandler(object sender, DataObjectPastingEventArgs e)
        {
            var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText);

            if (!isText) return;

            var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
            string t = "";


            foreach (char c in text.ToCharArray())
            {

                if (c == (char)160)
                {
                    // handle the character for later exports                
                }
                else
                {
                    t += c;
                }
            }
        }


What I have tried:

Well, I've looked at the content of the SourceDataObject of the DataObjectPastingEventArgs, specifically at the char values of the pasted text.
When I paste text from Word, all I get are regular spaces (32) instead of non-breaking spaces (160).
When I enter a non-breaking space into the textbox (alt + 0160) and I copy and paste this character, the non-breaking space is preserved.

I've also tried changing the DataFormat to no avail.


Thanks in advance for any help/hints

I also looked at the content of the clipboard directly but it's the same thing (not that I was really expecting otherwise).
Posted

1 solution

When the character is not present in the clipboard text but within the source, it is probably replaced when Word creates the clipboard objects.

Then the only solution would be not using the Unicode text clipboard format but another one. With Word, there should be at least RTF and HTML. However, these require processing the used format to convert the data to Unicode text.

For the HTML format see HTML Clipboard Format (Windows)[^] and HTML Clipboard Format (Internet Explorer)[^].

When your edit control is a RTF text box, using RTF might be the better choice but may require removing meta data (I have no Word installed here to check what is put to the clipboard).

RTF and HTML clipboard formats are both text formats. So you can inspect them with an editor that supports some kind of paste special option (like Notepad++).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900