简体   繁体   English

定时器,单击,mousedown,mouseup事件不能一起工作

[英]Timer, click, mousedown, mouseup events not working together

Looking for some help on a problem Im having 寻找一些问题的帮助我有

sorry if this question has already been asked, I can not find anything similar. 对不起,如果这个问题已被提出,我找不到类似的东西。

The idea is when a picturebox is clicked changed the image to ON. 这个想法是当点击图片框时将图像更改为ON。

If the picture box is held for more than 2 seconds to open a new form and leave the picturebox as OFF. 如果图片框保持2秒以上以打开新表格并将图片框保留为OFF。

However if the picturebox is clicked ON and then held for 2 seconds and then returns i need the picturebox state to remain ON. 但是,如果点击图片框然后保持2秒然后返回我需要图片框状态保持为ON。

Here is what I have tried so far. 这是我到目前为止所尝试的。

I believe for this to work correctly I need to stop MouseUp event from occuring. 我相信为了正常工作我需要阻止MouseUp事件发生。

Is there a way I can stop MouseUp when Tick occurs? 有什么方法可以在Tick发生时停止MouseUp吗?

Is there a easier / better way to do this? 有没有更简单/更好的方法来做到这一点?

Any help would be appreciated. 任何帮助,将不胜感激。

    private void time_HoldDownInternal_Tick(object sender, EventArgs e)
    { 
        time_HoldDownInternal.Enabled = false;
        time_HoldDownInternal.Interval = 1000;
        form1show.Visible = true;
    }

    private void pb_pictureBoxTest_MouseDown(object sender, MouseEventArgs e)
    {
        mainMenuVariables.mousedown = true;
        time_HoldDownInternal.Enabled = true;
    }

    private void pb_pictureBoxTest_MouseUp(object sender, MouseEventArgs e)
    {
        mainMenuVariables.mousedown = false;
        //MessageBox.Show("mouse up");
        time_HoldDownInternal.Enabled = false;
        time_HoldDownInternal.Interval = 1000;
    }

    private void pb_pictureBoxTest_Click(object sender, EventArgs e)
    {
        if (mainMenuVariables.mousedown == true)
        {
            if (mainMenuVariables.pictureBox == false)
            {
                mainMenuVariables.pictureBox = true;
                pb_pictureBoxTest.Image = new Bitmap(mainMenuVariables.pictureBoxOn);
                return;
            }
            if (mainMenuVariables.pictureBox == true)
            {
                mainMenuVariables.pictureBox = false;
                pb_pictureBoxTest.Image = new Bitmap(mainMenuVariables.pictureBoxOff);
                return;
            }
        }
        if (mainMenuVariables.mousedown == false)
        {
            //nothing
        }
    }

Rather than starting a timer, just record the current time on mouse down. 而不是启动计时器,只需记下鼠标按下时的当前时间。 Then in mouse up, check if it has been 2 seconds. 然后在鼠标中,检查它是否已经是2秒。 eg: 例如:

private void pb_pictureBoxTest_MouseDown(object sender, MouseEventArgs e)
{
    mainMenuVariables.mousedown = true;
    mainMenuVariables.mousedowntime = DateTime.Now;
}

private void pb_pictureBoxTest_MouseUp(object sender, MouseEventArgs e)
{
    mainMenuVariables.mousedown = false;
    var clickDuration = DateTime.Now - mainMenuVariables.mousedowntime;

    if ( clickDuration > TimeSpan.FromSeconds(2))
    {
        // Do 'hold' logic (e.g. open dialog, etc)
    }
    else
    {
        // Do normal click logic (e.g. toggle 'On'/'Off' image)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM