question

amirfarbodrm-0267 avatar image
0 Votes"
amirfarbodrm-0267 asked JackJJun-MSFT commented

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 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.

for a short question:
how to mouse down for 5second, and something happen? sorry for this rookie question, im new C# programmer & having some hard time.
ty

dotnet-csharpwindows-forms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@amirfarbodrm-0267, is any update? Please check if my answer works for you.

0 Votes 0 ·
JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered

@amirfarbodrm-0267,Welcome to Microsoft Q&A, you could try the following code to set a timer to mouse down for 5seconds and then show or hide the picture.

 public Form1()
         {
             InitializeComponent();
             timer1.Interval = 5000;
         }
    
         private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
         {
             timer1.Start();
          
         }
         int count = 0;
         private void timer1_Tick(object sender, EventArgs e)
         {
             if(count==0)
             {
                 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;
                 }
                 timer1.Stop();
                 count++;
             }
         }

Result:

191177-2.gif


Best Regards,
Jack



If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2.gif (361.4 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DavidLowndes-6766 avatar image
0 Votes"
DavidLowndes-6766 answered

Your approach of starting your timer in the mouse down event, killing it in a mouse up event, and when the timer expires, do what you need, should work.
So what aspect of it didn't work?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.