Cleared Clipboard is not null

时间:2015-09-25 16:36:38

标签: c# winforms clipboard

I want to check if the Clipboard consists of a data and if not, let the "Paste" Button be enabled. But unfortunately, even after I clear the Clipboard it still doesn't show it's null. I am working with Windows Forms.

I manually clear the clipboard:

private void button2_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();
        }

and then I add the following code to the Form LoadEvent:

if (Clipboard.GetDataObject() != null)
            {
                this.pn1_BtnPaste.Enabled = true;                  
            }

And it makes a button enabled which is weird to me. Can anybody explain why is that happening?

EDIT: Because I got understood wrong, let me change the code to make it more clear:

 private void button2_Click(object sender, EventArgs e)
        {
            Clipboard.Clear();

            if (Clipboard.GetDataObject() != null)
            {
                this.pn1_BtnPaste.Enabled = true;
            }
            else
                this.pn1_BtnPaste.Enabled = false;
        }

I click the "button2" and the "pn1_BtnPaste" is enabled anyway.

1 个答案:

答案 0 :(得分:1)

数据可以随时出现在剪贴板上。 Application.Idle事件是更新按钮状态的一种不错的方法:

jenkins-entrypoint.sh

当窗口关闭时,您必须再次取消订阅:

    public Form1() {
        InitializeComponent();
        Application.Idle += Application_Idle;
    }

Clipboard.GetDataObject()不像你想象的那样工作,它永远不会返回null。如果您想处理任何数据,那么您可以编写如下的事件处理程序:

    protected override void OnFormClosed(FormClosedEventArgs e) {
        Application.Idle -= Application_Idle;
        base.OnFormClosed(e);
    }

但很可能你会发现处理每种可能的格式都比你想象的要实用。