如何确定用户或代码是否单击了按钮?

时间:2018-02-13 13:30:57

标签: c# wpf

我正试图像这样解雇click event

bool m=false;
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("button1 clicke!");
        button1.Tag = "system";
        ButtonAutomationPeer peer = new ButtonAutomationPeer(button1);
        IInvokeProvider invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
        if (m==false)
            for (int i = 0; i < 2; i++)
            {
                invokeProv.Invoke();
                m = true;
            }
    }

我想只显示此MessageBox 3次。它有效但当我再次点击此按钮时,MessageBox只显示一次。

1 个答案:

答案 0 :(得分:2)

试试这段代码:

int i = 1;
private void button1_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("button1 clicked!");

    ButtonAutomationPeer peer = new ButtonAutomationPeer(button1);
    IInvokeProvider invokeProv = peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
    i++;
    if (i != 4)
    {
        invokeProv.Invoke();
    }
    else
    {
        i = 1;
    }
}