Stacked Clipboard


This article explains about simple clipboard operation and an extended clipboard functionality.

Introduction

Clipboard is a 
block of memory that can be used to store information temporarily. Most environments support a single clipboard transaction. Each cut or copy overwrites the previous contents. Normally paste operations copy the contents, leaving the contents available in the clipboard for further pasting operations.

clipboard

Stacked Clipboard

Modern GUIs often provide a clipboard manager which supports multiple cut and paste transactions. In this model the clipboard is treated as a stack or scrap book, with new cuts and copies being placed added to the top of the list of recent transations. The standard paste operation copies the most recent transaction, while specialized pastes provide access to the other stored transactions. These managers generally also provide a window that displays the transaction history and allows the user to select earlier copies, edit them, change their format and even search amongst them. 

Many of the applications don't provide a stack clipboard. This article explains how a stack clipboard can be implemented using menuitems in .Net.

Clipboard Operations

Lets see how we can use standard Clipboard to store and retrieve information using .Net.

We start by creating a windows application with a textbox and a context menu. To make the textbox a multiline, change its multiline property to true. The context menu is tied to the textbox by assigning the context menu property of the textbox. Now add items to the context menu with names copy, paste, copy special, paste special and delete special. The following sections explains all the functionalities in detail.

Copy

DataObject Class is used to provide a format-independent mechanism for data transfer. DataObject class is initailized first. To store the data in the DataObject SetData method is used. The arguments for this function are the format of the data, support  for automatic data conversion and the data respectively. In the copy menu's click event the following code is written.

DataObject m_data = new DataObject();
m_data.SetData(DataFormats.Text,true,textBox1.SelectedText);
Clipboard.SetDataObject(m_data,true);


The SetDataObject method of the Clipboard class copies the data from the DataObject to the Clipboard. The boolean parameter is to specify whether the data should remain on the clipboard after the application exits.

Paste

In the paste operation we copy the contents from the clipboard and paste it in to the textbox. GetDataObject().GetData() method of  Clipboard class is used to get the data from the clipboard. 

 

if(Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
    if(textBox1.SelectionLength == 0)
        textBox1.Text =  textBox1.Text.Insert(textBox1.SelectionStart,
        Clipboard.GetDataObject().GetData(DataFormats.Text).ToString());
    else
        textBox1.Text=textBox1.Text.Replace(textBox1.SelectedText,
        Clipboard.GetDataObject().GetData(DataFormats.Text).ToString());
else
    textBox1.Text  = "The clipboad does not contain any text";

 

Copy Special

Here we create menu item in the name of the selected text and assign it to the Paste Special menu and Delete Special menu . We dynamically create click events for the menu items to paste the text back to the textbox when that item is clicked and to delete the menu item when the delete special menu item is clicked.

 

MenuItem mi = new MenuItem(textBox1.SelectedText);
MenuItem mi1 = new MenuItem(textBox1.SelectedText);
mi.Click += new System.EventHandler(pasteSpecial_Click);
mi1.Click += new System.EventHandler(deleteSpecial_Click);
menuItem4.MenuItems.Add(mi);
menuItem5.MenuItems.Add(mi1);

When we copy more than one item, each text will be created as a menu item under the paste special menu which can be later used to paste the contents.

Paste Special

When an item in the Paste Special menu is clicked, the text in the menu item is pasted to the textbox. Using the sender's object we can get the menu item that recieved the click event. Menu item's text property can be used to retrieve the copied contents.

MenuItem mi=(MenuItem)sender;
if(textBox1.SelectionLength == 0)
    textBox1.Text = textBox1.Text.Insert(textBox1.SelectionStart,mi.Text );
else
    textBox1.Text=textBox1.Text.Replace(textBox1.SelectedText,mi.Text);

The above code pastes the copied contents to the textbox.

Delete Special

Delete Special is used to delete the menu items created under the paste menu. As we did in paste special, we use the sender's object to identify the menu item that recieved the click In paste special menu we use MenuItem.Remove method to remove the item from the paste special list. Then we use Dispose method to remove the item from the Delete Special menu.

MenuItem mi=(MenuItem)sender;
MenuItem mi1=(MenuItem)menuItem4.MenuItems[mi.Index];
menuItem4.MenuItems.Remove(mi1);
mi.Dispose();

Limitations

This copy special and paste special functionality is now limited to text data. This project can be improved to include image and other data also. This clipboard cannot be used in more than one applications. Happy programming!