Click here to Skip to main content
15,393,377 members
Articles / Operating Systems / Windows
Posted 30 Jul 2007


29 bookmarked

Code Rescue: Copying code from CodeProject to Visual Studio

Rate me:
Please Sign up or sign in to vote.
4.90/5 (16 votes)
30 Jul 2007CPOL7 min read
ow to copy code snippets with Internet Explorer while preserving newlines and indentation


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 or 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 with these browsers, 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 talk 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
  • User-defined data formats

This article deals with text formats only. The basic idea is to allow two programs to exchange data with as rich a content as they are both capable of. To that end, the program 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 you paste into 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 and HTML. ASCII and Unicode formats do not contain the necessary spaces, tabs and newline characters. 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 that 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. However, their ASCII and Unicode formats are correct. Note that Apple's Safari uses a linefeed character as a newline in its ASCII and Unicode formats and not the carriage return, or CRLF, that most of us would expect. However, 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 that WordPad and the RichTextBox Control do. If no RTF data is available, Visual Studio pastes either ASCII or Unicode text. We've already established that this is missing spaces, tabs and newlines when coming from Internet Explorer.

Bridging the gap

We've observed that 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 a RichTextBox at its heart. 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. 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.

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
  • An About window to top it all off

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

Publishing code on the CodeProject website

The CodeProject website lets you publish code snippets and offers some tags to organize it.

Screenshot - LP_IECodeRescue1.gif

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 and then paste your code in between the 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. My advice is that if 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. It also reduces both the quantity and the quality of the replies you may get!

Copying code from a CodeProject webpage into Visual Studio

With Internet Explorer, one must follows four steps to get it right:

  • Select: Obviously, you start by selecting the text you want. Let's 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 and the utility will 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.

Screenshot - LP_IECodeRescue3.gif

  • 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. Disabling it simply ensures that it will not conflict with some other application's hotkey.
    • The utility will play a sound: A low pitch indicates that nothing got changed -- e.g. because the clipboard did not hold the expected formats -- while 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.


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 such as spaces, tabs and 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.


Thanks go 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.


  • July 30, 2007 -- LP_IECodeRescue 1.2 (first public release)


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


About the Author

Luc Pattyn
Software Developer (Senior)
Belgium Belgium
I am an engineer with a background in electronics, software and mathematics.

I develop technical software, both for embedded systems and for desktop equipment. This includes operating systems, communication software, local networks, image processing, machine control, automation, etc.

I have been using all kinds of microcontrollers and microprocessors (Intel 4004/8080/8051/80386/Pentium, Motorola 680x/680x0/ColdFire/PowerPC, Microchip PIC, Altera NIOS, and many more), lots of programming languages (all relevant assemblers, Fortran, Basic, C, Java, C#, and many more), and different operating systems (both proprietary and commercial).

For desktop applications and general development tools I have been using both UNIX systems and Mac/MacOS for many years, but I have switched to x86-based PCs with Windows, Visual Studio and the .NET Framework several years ago.

I specialize in:
- cross-platform development (making software that runs on diverse hardware/OS combinations)
- instruction set simulation
- improving software performance, i.e. making sure the software runs the job at hand in as short a time as possible on the given hardware. This entails algorithm selection, implementation design, accurate measurements, code optimisation, and sometimes implementing virtual machines, applying SIMD technology (such as MMX/SSE), and more.

Comments and Discussions

GeneralCtrl+K+D Pin
GreenOrc6-Aug-07 19:47
MemberGreenOrc6-Aug-07 19:47 
GeneralRe: Ctrl+K+D Pin
Mark Salsbery7-Aug-07 10:11
MemberMark Salsbery7-Aug-07 10:11 
AnswerRe: Ctrl+K+D Pin
Luc Pattyn7-Aug-07 14:00
sitebuilderLuc Pattyn7-Aug-07 14:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.