定时器,鼠标​​,鼠标按下,鼠标松开事件不是一起工作 [英] Timer, click, mousedown, mouseup events not working together

查看:147
本文介绍了定时器,鼠标​​,鼠标按下,鼠标松开事件不是一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找一个问题进出口有



对不起,如果这个问题已经被问,我找不到类似的事情。

$ B $一些帮助b

我们的想法是,当点击一个图片被改变图像为ON。



如果图片框保持超过2秒开一种新的形式,并留下该图片为OFF。



但是,如果图片框上点击,然后保持2秒钟,然后返回我需要的PictureBox的状态留在。



下面是我到目前为止已经试过。



我相信这正常工作,我需要停下来从发生的历史MouseUp事件。



有没有一种方式,当勾选时我可以停止的MouseUp?



有没有更简单/更好的方式来做到这一点?



任何帮助,将不胜感激。

 私人无效time_HoldDownInternal_Tick(对象发件人,EventArgs五)
{
time_HoldDownInternal.Enabled = FALSE;
time_HoldDownInternal.Interval = 1000;
form1show.Visible = TRUE;
}

私人无效pb_pictureBoxTest_MouseDown(对象发件人,MouseEventArgs E)
{
mainMenuVariables.mousedown = TRUE;
time_HoldDownInternal.Enabled = TRUE;
}

私人无效pb_pictureBoxTest_MouseUp(对象发件人,MouseEventArgs E)
{
mainMenuVariables.mousedown = FALSE;
//MessageBox.Show(\"mouse了);
time_HoldDownInternal.Enabled = FALSE;
time_HoldDownInternal.Interval = 1000;
}

私人无效pb_pictureBoxTest_Click(对象发件人,EventArgs五)
{
如果(mainMenuVariables.mousedown ==真)
{
如果(mainMenuVariables.pictureBox ==假)
{
mainMenuVariables.pictureBox = TRUE;
pb_pictureBoxTest.Image =新位图(mainMenuVariables.pictureBoxOn);
的回报;
}
如果(mainMenuVariables.pictureBox ==真)
{
mainMenuVariables.pictureBox = FALSE;
pb_pictureBoxTest.Image =新位图(mainMenuVariables.pictureBoxOff);
的回报;
}
}
如果(mainMenuVariables.mousedown ==假)
{
//什么
}
}


解决方案

而不是启动一个定时器,只是记录鼠标当前的时间了。然后在小鼠,检查它是否已经2秒。例如:

 私人无效pb_pictureBoxTest_MouseDown(对象发件人,MouseEventArgs E)
{
mainMenuVariables.mousedown = TRUE ;
mainMenuVariables.mousedowntime = DateTime.Now;
}

私人无效pb_pictureBoxTest_MouseUp(对象发件人,MouseEventArgs E)
{
mainMenuVariables.mousedown = FALSE;
VAR clickDuration = DateTime.Now - mainMenuVariables.mousedowntime;

如果(clickDuration> TimeSpan.FromSeconds(2))
{
//做'持有'逻辑(例如,开放的对话,等等)
}
,否则
{
//完成正常的逻辑点击(如切换开/关的图像)
}
}


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.

If the picture box is held for more than 2 seconds to open a new form and leave the picturebox as 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.

Here is what I have tried so far.

I believe for this to work correctly I need to stop MouseUp event from occuring.

Is there a way I can stop MouseUp when Tick occurs?

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. e.g:

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)
    }
}

这篇关于定时器,鼠标​​,鼠标按下,鼠标松开事件不是一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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