DEV Community

Cover image for Build a ClipBoard Manager using Winforms
Olabamiji Oyetubo
Olabamiji Oyetubo

Posted on

2 1 2 1 1

Build a ClipBoard Manager using Winforms

If like me, you're a Software developer, you probably do a lot of copying and pasting. While in the zone working, you might copy several items and then struggle to recall where you copied a previous item from. Windows stores these copied items in its clipboard manager, but there's no built-in way to view your clipboard history. While extensions exist to solve this problem, I prefer creating my own solution, so that's what we're going to do today.

First, create a new Winforms (.NET Framework) project.

Create Project

Name your project ClipBoardManager and click Create

Scaffolded Project

Now, in the default form, add a new ListBox item. This will store the list of copied items.

ClipBoard ListBox

Rename the listBox1 item as ClipBoardList.

Rename ListBox

Next, create a class Helper that will store our clip board logic.

Helper Class

Replace the code in Helper with the below code;

 public class Helper : Form
 {
     [DllImport("user32.dll")]
     private static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);

     [DllImport("user32.dll")]
     private static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);

     private IntPtr nextClipboardViewer;

     public event Action<string> ClipboardUpdated;

     public Helper()
     {
         nextClipboardViewer = SetClipboardViewer(this.Handle);
     }

     protected override void WndProc(ref Message m)
     {
         const int WM_DRAWCLIPBOARD = 0x0308;

         if (m.Msg == WM_DRAWCLIPBOARD)
         {
             if (Clipboard.ContainsText())
             {
                 string text = Clipboard.GetText();
                 ClipboardUpdated?.Invoke(text);
             }
         }

         base.WndProc(ref m);
     }
 }
Enter fullscreen mode Exit fullscreen mode

We're using the Windows API (User32.dll) to detect changes in our environment. WM_DRAWCLIPBOARD listens for clipboard activity and extracts the text using a triggered event anything something is copied.

Now, in Form1, add the code below;

public partial class Form1: Form
{
    public Form1()
    {
        InitializeComponent();

        clipboardHelper = new Helper();
        clipboardHelper.ClipboardUpdated += OnClipboardChange;
    }

    private Helper clipboardHelper;
    private List<string> clipboardHistory = new List<string>();

    private void OnClipboardChange(string text)
    {
        if (!clipboardHistory.Contains(text))
        {
            if (clipboardHistory.Count >= 10)
                clipboardHistory.RemoveAt(0);

            clipboardHistory.Add(text);
            UpdateClipBoardList();
        }
    }

    private void UpdateClipBoardList()
    {
        ClipBoardList.Items.Clear();
        foreach (var item in clipboardHistory)
            ClipBoardList.Items.Add(item);
    }
}
Enter fullscreen mode Exit fullscreen mode

This initializes a new Helper class called clipboardHelper and then assigns ClipboardUpdated action to a new method OnClipboardChange. This method simply utilizies the UpdateClipBoardList method to update our ClipBoardList ListBox item.

And that's it. When you run the code, you get a simple UI, all you have to do is then copy a piece of text and it appears in our list.

Final Result

If you got lost somewhere along the line, the entire project can be found here

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (2)

Collapse
 
sashakrsmanovic profile image
Sasha

Very nice write up. I'm surprised you used WinForms though. Either way, thanks for sharing!

Collapse
 
bigboybamo profile image
Olabamiji Oyetubo

I use windows, so it just made sense for me to use the most windows compatible solution 😃

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay