简体   繁体   中英

MouseDown Event After Seconds

I'm working on a project that have a touchscreen monitor. I have 4 picturebox with two event(Mouse Down, Mouse click ). in this case, when i click on the picturebox another picture will visible.(this is easy part for me).
now i want to have a mouseDown event with this condition: if mousedown for 5second, a message box appear that have Yes or no, yes for Picturebox.visible = false; & No for Picturebox.visible = false; . i dont know how to do it.i tried to enable a timer for mouseDown and disable for mouse up. but not worked fine. how to mouse down for 5second, and something happen? sorry for this rookie question, im new in C# & having some hard time. :D

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
  timer1.Enabled = true;
  if(inTime)
  { 
    DialogResult dialogResult = MessageBox.Show("Sure?", "Hide the photo?", MessageBoxButtons.YesNo);
    if (dialogResult == DialogResult.Yes)
    {
      picturebox1.visible = false;
    }
    else if (dialogResult == DialogResult.No)
    {
      picturebox1.visible = true;
    }
  }
}

I hope that is what you meant.

int TimeToWait = 5;
DateTime DateTimeToFireTheMsgBox;

 

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{ 
    DateTimeToFireTheMsgBox = DateTime.Now.AddSeconds(TimeToWait);
    
    //timer1.Interval = 100; // milisec
    timer1.Start(); 
}
 
private void timer1_Tick(object sender, EventArgs e)
{
    if (DateTime.Now >= DateTimeToFireTheMsgBox )
    {
        timer1.Stop();
        
        DialogResult dialogResult = MessageBox.Show("Sure?", "Hide the photo?",
                                    MessageBoxButtons.YesNo);
        pictureBox1.Visible = dialogResult != DialogResult.Yes;
    }
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    timer1.Stop();
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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