Code Rescue: Copying code from CodeProject to Visual Studio

Home Page


Consultancy

  • Service Vouchers
  • Escrow Service

Shop



Programming
  • Articles
  • Tools
  • Links

Search

 

Contact

 

PHPinfo


$_SERVER







How to copy code snippets with Internet Explorer while preserving newlines and indentation

category 'tool', language C#, created 30-Jul-2007, version V1.2, by Luc Pattyn

This article also appeared on the CodeProject.


License: The author hereby grants you a worldwide, non-exclusive license to use and redistribute the files and the source code in the article in any way you see fit, provided you keep the copyright notice in place; when code modifications are applied, the notice must reflect that. The author retains copyright to the article, you may not republish or otherwise make available the article, in whole or in part, without the prior written consent of the author.

Disclaimer: This work is provided as is, without any express or implied warranties or conditions or guarantees. You, the user, assume all risk in its use. In no event will the author be liable to you on any legal theory for any special, incidental, consequential, punitive or exemplary damages arising out of this license or the use of the work or otherwise.


Download executable

Download source

Introduction

You all have seen code snippets both in CodeProject articles and CodeProject messages on one of the discussion boards. Sometimes they are formatted nicely, and sometimes they are not.

If your Internet Browser happens to be Netscape, FireFox, Safari, ... you may have no problems copying these code snippets from the CodeProject web page and pasting them into some application, Visual Studio or other; if so, the information in this article may still be of interest, but the utility presented will be of no value to you.

If on the other hand you use Microsoft Internet Explorer (version 6 or 7) or Maxthon, you probably do experience some problems: nicely formatted code snippets all too often loose their formatting, either the newlines, the indentation, or both. It all depends on circumstances, especially on how exactly you make your selection on the web page.

The Clipboard

Before we move to browsers and selection, we need to tell a bit more about the clipboard. This powerful system component is capable of handling all kinds of data; unknown to some, it can even store the same data in several formats at once. Here are some of these formats:

  • Text: that's regular ASCII text
  • UnicodeText: the same thing, now using Unicode characters
  • RTF: that's formatted text, using the Rich Text Format we all know from the RichTextBox control; it is WordPad's favorite, and it is well understood by many others, for instance MS Word.
  • HTML: again formatted text, this time using HTML (the Lingua Franca on the Internet)
  • other popular data formats for holding images, or other kinds of data
  • and even user-defined data formats.

This article deals with text formats only.

Now the basic idea is to allow two programs to exchange data with as rich a content as they are both capable of. In that end, the program where you copy from (not knowing what the destination is) will store the data on the clipboard in several formats, from simple text, to highly formatted text. The program where you paste, will normally consume the richest format that is available on the clipboard and supported by the program. That is how you can copy text with formatting from one MS Office program to another, and yet also paste it in Notepad.

Internet browsers

Each Internet browser copies text in its own way.

The different versions of Microsoft Internet Explorer seem to save text to the clipboard in the four text formats I mentioned before (ASCII, Unicode, RTF, HTML). Furthermore their ASII and Unicode formats do not contain the necessary spaces, tabs and newline characters; and the RTF format is used in a complex manner.

One other browser (Maxthon) seems to behave exactly like Internet Explorer: it copies the same four text formats to the clipboard, and has the same problems IE6 and IE7 have.

The majority of non-Microsoft browsers such as Netscape Browser, Mozilla FireFox and Apple's Safari do not emit RTF at all. But their ASCII and Unicode formats are correct.

Note: Apple's Safari uses a linefeed character as a newline in its ASCII and Unicode formats, and not the carriage return (or CRLF) most of us would expect; but that is no problem whatsoever for Visual Studio.

Visual Studio

As far as we could discover, when multiple formats are available, Visual Studio prefers the RTF format, but it does not always understand it in the same way WordPad and the RichTextBox Control do.

If no RTF data is available, Visual Studio pastes either ASCII or Unicode text; and we already established that is missing spaces, tabs and newlines when coming from Internet Explorer.

Bridging the gap

We observed WordPad does a better job at pasting CodeProject code snippets, than Visual Studio does. So we need to somehow modify and improve what is on the clipboard, before we paste it into Visual Studio.

The solution to that is a little program with at its heart a RichTextBox: if the conditions are right (that is if the four text formats are present so the source is likely Internet Explorer), we paste the clipboard's RTF data in it, and then copy it back from it; this simple operation results in a much simpler RTF representation of the same text, so much so that Visual Studio understands it completely now, including the newlines, and the indentation (for Internet Explorer: only if copied correctly to begin with).

So the LP_IERescueCode utility program is a small Windows application, with:

  • an invisible main form containing a RichTextBox;
  • a NotifyIcon that offers a context menu to deal with some settings;
  • a hotkey to increase its ease of use;
  • and an About window to top it all of.

It is not the intention of this article to describe the program any further; the source code is available for whom is interested.

Publishing code on the CodeProject web site

The CodeProject web site lets you publish code snippets, and offers some tags to organize it.

Sample result of a PRE block

This is what we recommend:

  • use the PRE tags (that is <PRE> and </PRE> ); you can get these tags by clicking on the PRE label underneath the edit box when entering a CodeProject message, or you can just type them yourself; then paste your code in between these PRE tags.
  • what the PRE block does for you is all right; it temporarily switches to:
    • a non-proportional font,
    • a colored background,
    • a mode that preserves formatting (i.e. spaces, tabs and newlines do not get eaten by the HTML formatting and/or the browser).
  • there are some points to be careful about:
    • an empty line (that's two consecutive newlines) will disappear; you should add a space to it, to keep it as intended.
    • every < sign will be investigated; if it looks like the start of an HTML tag, it will not show; so my advice is: as soon as the first non-whitespace character following the < sign is a letter, replace the < sign by the four characters &lt;
  • try not to add tags inside the PRE block:
    • it is OK to add <B> and </B> tags to get some bold text.
    • do not add <CODE> </CODE> tags.
Without the PRE tags, indentation is lost, making it much harder to read the code snippet, and reducing both the quantity and the quality of the replies you may get !

Copying code from a CodeProject web page into Visual Studio

With Internet Explorer one needs four steps to get it right:

  • Select: Obviously you start by selecting the text you want. Lets hope it is inside a PRE block as explained above. However there is more:
    • with Microsoft Internet Explorer (and Maxthon), you should select entire lines only, which you can achieve by dragging in the left margin. Failing to select entire lines will loose all indentation, the utility would not be able to cure that.
    • Often the above is sufficient, but you can increase your chances by selecting up to the last line of the PRE block. If the last line of your selection also marks the end of a PRE block, do not be surprised or worried about a little extra selection marker showing up somewhat below. That is all right.

Selecting a few lines upto the end of the PRE block

  • Copy: Copy as you normally would (probably using CTRL/C).
  • Rescue: if using Microsoft Internet Explorer, let the LP_IECodeRescue utility perform its magic:
    • either click its "Code Rescue" menu item (in the context menu of the NotifyIcon), or just hit the hotkey, which is CTRL/F2 but can be disabled (so it would not conflict with some other application's hotkey).
    • The utility will play a sound: a low pitch indicates nothing got changed (e.g. because the clipboard did not hold the expected formats), a high pitch indicates a successful RTF replacement. The sounds are mapped onto the system's Exclamation and Beep sounds respectively; they also can be switched off using the context menu.
  • Paste: Paste as you normally would (probably using CTRL/V).

Conclusion

When a code snippet appears perfectly formatted on the CodeProject web page, you can copy/paste it to Visual Studio with any web browser you choose while preserving its formatting (spaces, tabs, newlines). If you want to do this using Microsoft Internet Explorer or Maxthon, you should carefully select a number of entire lines, up to the end of the PRE block, and get the clipboard content processed by the LP_IECodeRescue utility.

Acknowledgements

Thanks to several CodeProject members, and in particular to Paul Conrad, George L. Jackson, and Mark Salsbery who helped a lot in pinpointing the actual problem, testing the utility and improving this article.

History

  • LP_IECodeRescue 1.2 (first public release).


Perceler

Copyright © 2012, Luc Pattyn

Last Modified 21-May-2025