user11243368
user11243368

Reputation:

Clear clipboard on form close C#

when I close my program, it will execute my last clipboard.gettext when I paste it. I want when I close the program that no longer carries out that last clipboard.gettext command.

What I've already tried is to encode this code behind my wndproc but that didn't help me any further:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    ChangeClipboardChain(this.Handle, _clipboardViewerNext);        // Removes our from the chain of clipboard viewers when the form closes.
}

And this is code of

       [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
        private IntPtr _ClipboardViewerNext;

        //Make some global variables so we can access them somewhere else later
        //This will store all Questions and Answers
        //In here will be the Questions and Answers
        List<question> questionList = new List<question>();
        // Demonstrates SetText, ContainsText, and GetText.



        private void Form1_Load(object sender, EventArgs e)
        {
            //Set our application as a clipboard viewer
            _ClipboardViewerNext = SetClipboardViewer(Handle);
            this.FormClosing += Form1_FormClosing;

            //Add question/answer to list
            //hoofdstuk 3 it
            question newQuestion = new question("wat is de hoofdstad van nederland?", "Amsterdam.*");

        }

        private void GetAnswer(string clipboardText)
        {
            //Loop through all questions and answers
            foreach (question q in questionList)
            {
                //If we have found an answer that is exactly the same show an Notification
                if (q._question == clipboardText)
                {
                    ShowNotification(q._question, q._answer);
                }
            }
        }

        private void ShowNotification(string question, string answer)
        {
            notifyIcon1.Icon = SystemIcons.Exclamation;
            notifyIcon1.BalloonTipTitle = question;
            notifyIcon1.BalloonTipText = answer;
            notifyIcon1.BalloonTipIcon = ToolTipIcon.Error;
            Clipboard.Clear();
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            {
                const int WM_DRAWCLIPBOARD = 0x308;
                if (m.Msg == WM_DRAWCLIPBOARD)
                {
                    GetAnswer(Clipboard.GetText(TextDataFormat.UnicodeText));
                }

            }
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Clipboard.Clear();
        }
    }
}


Thanks in advance.

Upvotes: 0

Views: 893

Answers (2)

Anu Viswan
Anu Viswan

Reputation: 18155

You could use

 Clipboard.SetDataObject(string.Empty);

Complete Code

this.FormClosing += (s, ev) => { Clipboard.SetDataObject(string.Empty); };

Upvotes: 0

DxTx
DxTx

Reputation: 3347

Have tried using Clipboard.Clear() method..?

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    Clipboard.Clear();
}

Full C# code

using System;
using System.Windows.Forms;

namespace WindowsTestForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Clipboard.SetText("Testing...");
            this.FormClosing += Form1_FormClosing;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //Clipboard.SetText(" "); // Value cannot be null
            Clipboard.Clear();
        }
    }
}

Resources

Upvotes: 1

Related Questions