如何禁用和启用MouseEventArgs后......? [英] How to disable and after enable the MouseEventArgs...?

查看:103
本文介绍了如何禁用和启用MouseEventArgs后......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我想将MouseEventArgs重置为始终只运行一次。

如果有人多次点击按钮(10次),那么程序运行10次,依此类推。





我的代码不能正常工作,因为如果有人点击按钮1次或多次就会运行它。

我想重置MouseEventArgs或做其他解决方案。



 私人  void  button1_MouseClick ( object  sender,MouseEventArgs e)
{

button1.Enabled = false < /跨度>;
button1.MouseClick - = button1_MouseClick;
mc ++;


if true
{
button1.Enabled = false ;
// button1.Location = new Point(40,40);
// 点位置= button1.Location;
// location.X = 0; location.Y = 0;
System.Threading.Thread.Sleep( 2000 );
if (mc == 1
{

// button1.Location = new Point(67,191);
// location.X = 67; location.Y = 191;
mc = 0 ;
button1.Enabled = true ;
button1.MouseClick + = button1_MouseClick;

textBox1.AppendText( 点击 + e.Clicks + ,点击: + mc + < span class =code-string> \ n
);

}
}
}

解决方案

  public  Form1()
{
InitializeComponent();
}

int numberOfTimesButtonPressed = 0 ;
private void button1_Click( object sender,EventArgs e)
{
button1.Enabled = false ;
Application.DoEvents();

numberOfTimesButtonPressed ++;
System.Threading.Thread.Sleep( 5000 );

Application.DoEvents();
button1.Enabled = true ;
}

私有 void Form1_FormClosing( object sender,FormClosingEventArgs e)
{
MessageBox.Show(numberOfTimesButtonPressed.ToString());
}





此按钮现在只能按一次,直到方法完成。


我对此很感兴趣,因为我预计一旦取消注册MouseClick事件就会忽略点击次数。



但是,我使用 Application.DoEvents让它工作 [ ^ ]方法

  private   void  button1_MouseClick( object  sender,MouseEventArgs e)
{
button1.Enabled = ;

mc ++;

// 如果你正在做一些长时间运行的东西,请考虑运行它
// 在另一个线程上异步。
System.Threading.Thread.Sleep( 2000 );

// 注意e.Clicks将始终显示1
textBox1.AppendText( 点击 + e.Clicks + ,点击: + mc + \\\
);

// 消耗任何已缓存的事件
< b> Application.DoEvents();

// 最后重新启用按钮
button1.Enabled = true ;
}



注意不需要那个 if(true)语句并且你也在重置每次点击次数


Hi Everyone!

I wanna reset the MouseEventArgs to always run only once.
If somebody click the button more than once (10 times) then the program run 10x and so on.


My code doesn't work well, because if someone click on the button1 2x or more times it will be runned.
I wanna reset the MouseEventArgs or do other solution.

private void button1_MouseClick(object sender, MouseEventArgs e)
        {

            button1.Enabled = false;
            button1.MouseClick -= button1_MouseClick;
            mc++;


            if (true)
            {
                button1.Enabled = false;
                //button1.Location = new Point(40, 40);
                //Point location = button1.Location;
                //location.X = 0; location.Y = 0;
                System.Threading.Thread.Sleep(2000);
                if (mc == 1)
                {

                    //button1.Location = new Point(67, 191);
                    //location.X = 67; location.Y = 191;
                    mc = 0;
                    button1.Enabled = true;
                    button1.MouseClick += button1_MouseClick;

                    textBox1.AppendText("Click " + e.Clicks + ", Clicks: " + mc + "\n");

                }
            }
        }

解决方案

public Form1()
    {
        InitializeComponent();
    }

    int numberOfTimesButtonPressed = 0;
    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        Application.DoEvents();

        numberOfTimesButtonPressed++;
        System.Threading.Thread.Sleep(5000);

        Application.DoEvents();
        button1.Enabled = true;
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        MessageBox.Show(numberOfTimesButtonPressed.ToString());
    }



This button can now only be pressed once, until the method is finished.


I was intrigued by this as I expected the clicks to be ignored once you had de-registered the MouseClick event.

However, I got this to work using the Application.DoEvents[^] method

private void button1_MouseClick(object sender, MouseEventArgs e)
{
    button1.Enabled = false;

    mc++;

    // If you are doing some long running stuff consider running it 
    // asynchronously on another thread.
    System.Threading.Thread.Sleep(2000);

    // Note e.Clicks will always show 1
    textBox1.AppendText("Click " + e.Clicks + ", Clicks: " + mc + "\n");

    // Consume any events that have been cached
    Application.DoEvents();
    
    // finally re-enable the button
    button1.Enabled = true;
}


Note there was no need for that if(true) statement and you are also resetting the click count each time


这篇关于如何禁用和启用MouseEventArgs后......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆