Click here to Skip to main content
6,292,426 members and growing! (9,853 online)
Email Password   helpLost your password?
General Programming » Macros and Add-ins » Visual Studio .NET Addins     Intermediate License: The Code Project Open License (CPOL)

wiicwgp (what is in the clipboard is what get pasted) Add-in

By Rama Krishna Vavilala

An add-in that allows HTML element text to be pasted exactly as it is in the clipboard, disallowing VS.NET to add any extra attributes.
C++, C#, Windows, .NET 1.0, .NET 1.1VS.NET2003, Dev
Posted:17 May 2003
Views:40,306
Bookmarked:14 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
19 votes for this article.
Popularity: 6.07 Rating: 4.74 out of 5

1

2

3
2 votes, 11.8%
4
15 votes, 88.2%
5

Introduction

Chris Maunder posted in the Lounge about what he considers an annoying feature in VS.NET, that it adds ID attribute to an HTML element when pasted from a clipboard. (Actually, it adds the ID attribute only to some subset of elements like the INPUT element.) Here is what you need to see the feature in action:-

  1. Cut/Copy some HTML text like <input name="test" value="">
  2. Paste it in the HTML source editor. The text that will be pasted will be <input name="test" value="" id=Text1>. The text in the clipboard is still <input name="test" value=""> which can be verified by pasting in any other text editor.

The purpose of this add-in is to not allow the above to happen.

Solution

Fortunately, Visual Studio .NET Extensibility API is quite rich and provides an amazing level of control. The solution turned out to be pretty simple. Visual Studio .NET fires two events before any command get executed AfterExecute and BeforeExecute. The BeforeExecute event allows the handlers to optionally cancel the command execution. So the add-in can handle the BeforeExecute event and write the text as it is and cancel the default command execution. Here is a detailed description of the add-in code.

  1. The first step is to add the BeforeExecute event for the Edit.Paste command. For this we need to find the Guid and ID of the command that Visual Studio .NET maintains for the command. This can be obtained from the command object corresponding the the Edit.Paste command. The following code gets the Command object.

    Command cmd;
    cmd = applicationObject.Commands.Item("Edit.Paste", 0);
  2. Next we need to handle the BeforeExecute event of this command.

    CommandEvents events;
    events = applicationObject.Events.get_CommandEvents(cmd.Guid, cmd.ID);
    events.BeforeExecute += new 
        _dispCommandEvents_BeforeExecuteEventHandler(this.OnBeforePaste);
  3. Now before any text is pasted, the control goes to the OnBeforePaste method which looks like:

    void OnBeforePaste(string guid, int id, 
       object customIn, object customOut, ref bool cancel)

    If the cancel parameter value is set to true in the function, the command execution will be canceled.

  4. When OnBeforePaste function is called, we need to check whether the user is using the HTML editor. Here is how this is done.
    HTMLWindow htmlWindow = 
         applicationObject.ActiveWindow.Object as HTMLWindow;
    
    if (htmlWindow != null)
    {
        //This means that user is in the HTML editor
  5. Finally we need to add text on our own to the document and cancel the default paste operations.
    if (htmlWindow.CurrentTab == vsHTMLTabs.vsHTMLTabsSource)
    {
        IDataObject dataObject = Clipboard.GetDataObject();
        string text = (string)dataObject.GetData(DataFormats.Text);
        TextWindow tw = (TextWindow)htmlWindow.CurrentTabObject;
        tw.Selection.Insert(text, 
            (int)vsInsertFlags.vsInsertFlagsInsertAtStart);
    }
    else
    {
        mshtml.IHTMLDocument2 doc = 
           (mshtml.IHTMLDocument2)htmlWindow.CurrentTabObject;
        doc.execCommand("Paste", false, null);
    }
  6. Finally the event handlers need to be removed in the OnDisconnection event of the add-in.
    events.BeforeExecute -= new 
      _dispCommandEvents_BeforeExecuteEventHandler(this.OnBeforePaste);

Using the add-in

Follow these simple steps to start using the add-in:-

  1. Download the add-in setup and install it.
  2. Enable the add-in using the add-in manager (by default it is not loaded). Any HTML cut-paste operations will not add the default ID attribute.
  3. Unloading the add-in will make cut-paste operations work in the default way.

Updates

  • May 19, 2003 - Added support for VS.NET 2003 and also support for editing from HTML design view

License

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

About the Author

Rama Krishna Vavilala


Member

Occupation: Architect
Location: United States United States

Other popular Macros and Add-ins articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralOverwrite problems Pinmemberfiendiest14:18 20 Jun '05  
Generalany chance of a new version? Pinmemberorangenick23:59 11 Oct '04  
GeneralStill not working in VS.Net 2003 Pinmemberg_byte7:11 2 Sep '04  
GeneralIT DOES NOT WORK!!!!!!!!! Pinmembercoolman545310:56 17 Aug '04  
GeneralPrevious Changes + Atomic Undo Pinmemberlenwilcox8:33 17 Mar '04  
GeneralAnother new feature! Pinmemberultplyr12:18 28 May '03  
Generalfeature request PinsussAnotherUser6:10 22 May '03  
GeneralRe: feature request PinmemberRama Krishna8:18 22 May '03  
GeneralIf this works... PinsitebuilderPaul Watson22:30 18 May '03  
GeneralRe: If this works... PinsitebuilderPaul Watson22:51 18 May '03  
GeneralRe: If this works... PinmemberRama Krishna3:11 19 May '03  
GeneralRe: If this works... PinmemberRama Krishna5:06 19 May '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 May 2003
Editor: Smitha Vijayan
Copyright 2003 by Rama Krishna Vavilala
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project