C# Programming > Windows Forms

C# Clipboard Event Textbox

 

Clipboard Events

A Textbox in C# has a number of useful events to indicate when certain actions have been taken. For example, .NET textboxes have an event to indicate when the text has changed or when the user has pressed a key. These events allow C# developers to write clean code that interacts with textboxes.

Following the same principles, we can manually implement events that are triggered by clipboard actions, i.e. text is cut, copied, or pasted in the textbox. The .NET Framework does not come with these events, but they are not difficult to implement.

7/5/11 Update: Added support to suppress copy, cut, and paste events.

Custom Textbox

To implement custom events, we are going to have to create our own textbox user control. The user control will inherit the Textbox class since we want all the default behaviors of a .NET textbox.

Creating a custom user control will also let us override the WndProc function, which processes messages passed to the control. By overriding the function, we can detect messages such as when text is cut, copied, or pasted, before allowing the control to process them.

Textbox Cut Event

Detecting each of the clipboard messages will be the same. Specifically for the cut message, we want to compare the message ID to the WM_CUT constant:

private const int WM_CUT = 0x0300;
protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_CUT)
    {
        //Cut Event
    }
}

We will discuss how to trigger the event below. For now, the important thing to consider is how to figure out what text is being cut. For this to a useful event, we want to give the developer the text which is being cut.

Remember that we are processing the message before it is applied. Which means that the text to be cut is not in the clipboard yet. Thus to access the text that will be cut we need to look directly at the control. Luckily textboxes have a SelectedText property which gives us the value we are looking for.

Textbox Copy Event

The copy event will work almost the same, the only difference will be the WM_COPY constant:

private const int WM_COPY = 0x0301;
protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_COPY)
    {
        //Copy Event
    }
}

The same concept applies here as well, the text select has not been copied yet, so it is not in the clipboard yet. The SelectedText property will give the text that will be copied.

Textbox Paste Event

Similarly, the paste event will be triggered when the WM_PASTE constant is matched:

private const int WM_PASTE = 0x0302;
protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_PASTE)
    {
        //Paste Event
    }
}

In this case however, since the text is being pasted, it already exists on the clipboard. It does not yet exist on the textbox. To access the text we need to use the Clipboard.GetText() function.

Raising the Events

The "tricky" part to consider when raising our textbox cut, copy, and paste events, is how to pass the text values we figured out above (from SelectedText or Clipboard.GetText()).

Luckily for us, the .NET Framework has a built-in EventArgs class which is designed to do just that. By default, EventArgs gives basic information about the raised event. But by creating a class that inherits EventArgs, we can add custom properties.

So for example, in our case, we want to set the clipboard text (whether it is going to exist in the clipboard or it already does):

public class ClipboardEventArgs : EventArgs
{
    public string ClipboardText
    {
        get;
        set;
    }

    public ClipboardEventArgs(string clipboardText)
    {
        this.ClipboardText = clipboardText;
    }
}

With this we can use delegates to define and raise our events. Here is a quick example of how to raise the textbox paste event:

public delegate void ClipboardEventHandler(object sender, ClipboardEventArgs e);
public event ClipboardEventHandler PastedText;

//Raising the event
if (PastedText != null)
    PastedText(this, new ClipboardEventArgs(Clipboard.GetText()));

I will not get into the details but it is a straightforward event definition and call. If you do not know what is going on, download and take a look at the source code at the bottom of the page.

Suppressing Copy, Cut, and Paste

We can also suppress each of the clipboard events. This is done by not passing the message along to the original WndProc function. Usually we always want to call base.WndProc(m) since we can't process every message that goes through the control. By omitting the call, we stop the control from performing the specific message (copy, cut, and paste in this case).

Conclusion

The result is a custom textbox that has events that indicated when text is being cut, copied, or pasted in the textbox. The clipboard events can be used like any other C# textbox event. Download the source code below to see how it all comes together:

Back to C# Article List