Clipboard History Plug-in for Visual Studio with the DXCore, Part 1

Hey, let's create a new plug-in that shows the recent history of clipboard operations using the DXCore, a free and powerful extensibility engine for Visual Studio. If you don't already have the DXCore installed (it's already installed if you have CodeRush or Refactor! Pro), you can get it here.

In this series of daily blog posts we'll go from rough idea to a finished, polished feature, which will eventually ship with CodeRush (including full source). So not only will this series show you how to build plug-ins for Visual Studio, but it will also give you some insight into the creative process we go through here at Developer Express as we build killer developer tools for you.

In this, the first post of the series, you'll learn how to:

  • Create plug-in projects using the DXCore's helpful wizards.
  • Add Actions (commands) to your plug-in.
  • Bind keyboard shortcuts to your commands.
  • Constrain feature availability with context.
  • Handle Visual Studio events.
  • Send text out to the Visual Studio status bar.

I think you'll find all of this easy to follow and even easier to implement. So sit back and enjoy as we start the journey to an exciting and powerful new feature....

Getting Started

From the DevExpress menu, select New Plug-in...


Select a language and specify the name of the plug-in. I like to prefix features that go into CodeRush with "CR_", so I'm doing that here...

New DXCore Plugin Project

Click OK.

The next dialog lets you specify settings for the project. We can accept all the defaults here.


Click OK.

Adding an Action

Next, let's add an action so we can invoke our clipboard history with a keyboard shortcut or menu item. From the DXCore section of the Toolbox, select the "Action" item...


then drop it onto the design surface...


and set the following properties:

Property Value Comments
(Name) actClipboardHistory We'll give our "action1" a meaningful name.
ActionName ClipboardHistory We can bind this action to a shortcut later.
Description Shows a history of recent clipboard operations. This description will show up in the User Guide later when the plug-in is loaded.

The Properties grid should now look like this:


Now switch to the Events view:


Double-click the Execute event handler....


Let's add some quick test code so we can quickly verify everything is working.

private void actClipboardHistory_Execute(ExecuteEventArgs ea)
  MessageBox.Show("Show clipboard history.");

Listening to ClipboardChanged

Let's also listen to the event that fires whenever the clipboard's content changes....


Activate the design surface.


Click the design surface itself (so no controls are selected).


Switch to the Events view of the Properties grid.


Double-click the ClipboardChanged event.


Let's send some text out to the status bar, just so we can have some visual confirmation that the event is firing when we expect it to.

private void PlugIn1_ClipboardChanged(ClipboardChangedEventArgs ea)
  string clipboardText = ea.Details.Text;
  if (String.IsNullOrEmpty(clipboardText))
  int lineBreakPos = clipboardText.IndexOf(Environment.NewLine);
  if (lineBreakPos > 0)
    clipboardText = clipboardText.Substring(0, lineBreakPos) + " ...";
  CodeRush.ApplicationObject.StatusBar.Text = "Clipboard: " + clipboardText;

Quick Test

Now let's test what we have so far....

Click Run.


This will start up a second instance of Visual Studio.

Important: If you're stepping through code in an instance of Visual Studio that has the DXCore loaded, it is possible to lockup both instances of Visual Studio if you change the contents of the clipboard. This can happen because each instance of the DXCore participates in the clipboard chain, and the instance you're stepping through is unable to respond to clipboard chain messages.

Binding a Shortcut to our ClipboardHistory Action

First, let's assign a shortcut binding to our new ClipboardHistory Action in the newly started instance of Visual Studio....

From the DevExpress menu, select Options...


Open the IDE folder and select the Shortcuts options page....


Creating a Custom Shortcuts Folder

If you don't already have a folder for custom shortcuts, click the New Folder button...


Give the folder a good name, and make it a top-level folder...


Creating a New Shortcut

Now with your new shortcuts folder selected and ready to hold all your custom shortcuts, click the New Keyboard Shortcut button....


The Key 1 TextBox gains focus....


Press Ctrl+Shift+Insert.


This will be the shortcut we press to invoke the clipboard history.

Tab down to the "Command:" combo box.... Type in the first few letters of our ClipboardHistory action.


Our action name should auto-fill, our first indication that our new clipboard history plug-in has loaded successfully.

If we don't specify a context for this Ctrl+Shift+Insert command binding, then this shortcut binding will be valid and work anywhere inside Visual Studio (even when modal dialogs are up). I'm comfortable with that for now.

Applying Constraints with the Context Picker

If we wanted to limit this shortcut to a more constrained context, we could specify those constraints in the context picker. For example, if we wanted this binding to only work when the code editor had focus, we would give the Focus\Documents\Source\Code Editor context a green check, which means this condition must be satisfied (the editor must have focus).


Note that if you click the condition a second time, the green check will turn into a red X, indicating that the condition must be false in order for the binding to be valid.

Clicking the condition a third time will cycle it back to an empty box, which means the context is ignored. It is possible to combine contexts in very sophisticated ways, so you can precisely define the state in which the shortcut binding is allowed to function.

Click OK to close the DevExpress Options dialog (and we'll leave that context empty so the shortcut binding works anywhere).

Testing the Shortcut Binding

Press Ctrl+Shift+Insert, and if everything is working as expected, we'll see our dialog:


Great! We've successfully bound a shortcut to our new Action. We'll fill that method with real code in a bit.

Testing the ClipboardChanged Handler

Next, let's test the code we added in the ClipboardChanged event handler. Because this event fires whenever the contents of the clipboard changes, regardless of from where the clipboard change takes place, you should be able to select the title of this section ("Testing the ClipboardChanged Handler") in your browser and then copy that to the clipboard, and see the status bar in Visual Studio change. Try that now. You should see something like this in the instance of Visual Studio that we're testing:


Excellent! The ClipboardChanged event is firing as expected.

Shutting Down the Test Instance of Visual Studio

Important: When debugging two instances of Visual Studio like this, if the first instance (the debugger) has the DXCore loaded, then we strongly recommend you close down the second instance by clicking the red X close button in the upper right...


or by selecting File | Exit from Visual Studio. This will allow the second instance of the DXCore to shut down properly, which includes the necessary de-registration of the DXCore's clipboard hook.  

Wrapping Up the First Part

Congratulations! We've just completed the first, most important step of the process, setting up the plug-in framework.

In tomorrow's post, we'll dive more into the functional implementation details of the clipboard history. See you then!

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.