Timer, click, mousedown, mouseup events do not work together

Seek help on Im having

Sorry if this question has already been asked, I cannot find anything like it.

The idea is that clicking on the camera changes the image to ON.

If the image window is held for more than 2 seconds to open a new form and leave the image as OFF.

However, if the On button is pressed on the camera, and then held for 2 seconds and then returns, I need the cameras to remain on.

Here is what I have tried so far.

I believe that for proper operation I need to stop the MouseUp event.

Is there a way to stop MouseUp when Tick appears?

Is there an easier / better way to do this?

Any help would be greatly 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
        }
    }
+3
1

. , , 2 . :

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

All Articles