vlovystack
vlovystack

Reputation: 703

Simple Timer in C#

I have this code

private void picTop_MouseEnter(object sender, EventArgs e)
{
  if (timer1.Tick == 10)
  {
    picBottom.Visible = true;
    picTop.Visible = false;
    timer1.Stop();
  }
  else 
  {
    MessageBox.Show("ERROR You cannot view this section at this time.\nPlease try again later.");
  }
}

private void picBottom_MouseEnter(object sender, EventArgs e)
{
  picBottom.Visible = false;
  picTop.Visible = true;
  timer1.Start();
}

My timerinterval is set at 1000ms (so 1 second) I only want the user to go into the top panel again after 10 seconds. Some help would be greatly appreciated.

Current error I get: timer1.Tick is red underlined, error= "The event 'System.Windows.Forms.Timer.Tick' can only appear on the left hand side of += or -="

Upvotes: 0

Views: 3225

Answers (2)

Asif Mushtaq
Asif Mushtaq

Reputation: 13150

Timer.Tick is not property its an event.

Use it like

timer1.Tick += 
{
    picBottom.Visible = true;
    picTop.Visible = false;
    timer1.Stop();
}

For interval use timer.Interval

timer.Interval = 10000;

Upvotes: 2

Eoin Campbell
Eoin Campbell

Reputation: 44306

Ok. I think I understand what you're trying to achieve...

You have 2 areas on your form called "Top" & "Bottom"

Once the user enters & subsequently leaves the top area, you don't want them to be able to enter again for 10 seconds. is that correct?

So you've got a few problems... first of all, Tick is an event to which you would attach a method to be fired when it is raised. it's not an integer you can check. The only integer property on a timer of relevance for timing for is called Interval

But aside from that I don't think your method is going to be particularly effective. Perhaps a better idea would be to add a MouseExit event to the top area. and disable that area for 10 seconds. and use a timer to re-enable it.

timer1.Tick += timer1_Tick;

public void Top_MouseExit (object sender, EventArgs e)
{
   PicTop.Visible = false; // or hide/disbale it some other way
   Timer1.Interval = 10000; //10 seconds
   Timer1.Start();
}

public void timer1_Tick(object sender, EventArgs e)
{
    timer1.Stop();
    PicTop.Visible = true; //renable the top area
}

Upvotes: 1

Related Questions