marek_lani
marek_lani

Reputation: 4123

C# make panel visible for specific time after mouse move over other panel

I need to make panel visible for specific time after mouse move over other panel I have been solving this problem quite long. I tried to use Timer but I wasn't successful.

This is my code:

 this.MouseHover += new EventHandler(myMouseHover);

   [...]
   //event handler
   private void myMouseHover(object sender, EventArgs e)
        {

                this.prevPanel.Visible = true;
                this.nextPanel.Visible = true;

                /* here I want put timer */

                this.prevPanel.Visible = false;
                this.nextPanel.Visible = false;

        }

Upvotes: 0

Views: 3341

Answers (3)

m4ngl3r
m4ngl3r

Reputation: 560

System.Timers.Timer timer1 = new System.Timers.Timer ();

timer1.Interval  = periodAfterToStopInMiliseconds;

timer1.Elapsed += timer1_Elapsed;

private void ActLikeIWant(double periodAfterToStopInMiliseconds)
{
    timer1.Start();
}

void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
     var timer = (System.Timers.Timer) sender;

     timer.Stop();

     this.prevPanel.Visible = false;
     this.nextPanel.Visible = false;
}

private void myMouseHover(object sender, EventArgs e)
{
    this.prevPanel.Visible = true;
    this.nextPanel.Visible = true;

    ActLikeIWant(periodAfterToStopInMiliseconds: 200);
}

Upvotes: 0

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

You can use Timer and define Interval property

Property :

private System.Windows.Forms.Timer aTimer;

Init :

    aTimer = new System.Windows.Forms.Timer();

    // Hook up the Elapsed event for the timer.
    aTimer.Tick += new ElapsedEventHandler(OnTimedEvent);

    // Set the Interval to 2 seconds (2000 milliseconds).
    aTimer.Interval = 2000;
    aTimer.Enabled = true;

Delegate :

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
      this.prevPanel.Visible = false;
      this.nextPanel.Visible = false;
}

Upvotes: 1

Matt Burland
Matt Burland

Reputation: 45135

Depending on the type of timer you are using, you need to put the code that will hide your panel in response to the tick event.

For example, using the System.Windows.Forms.Timer:

System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();

// form constructor
public myForm() 
{
    myTimer.Interval = 1000;    // or whatever you need it to be
    myTimer.Tick += new EventHandler(TimerEventProcessor);   
}

private void myMouseHover(object sender, EventArgs e) 
{
     this.prevPanel.Visible = true;
     this.nextPanel.Visible = true;
     myTimer.Start();
 }

private void TimerEventProcessor(Object myObject, EventArgs myEventArgs) {
     myTimer.Stop();
     this.prevPanel.Visible = false;
     this.nextPanel.Visible = false;
}

There are other timers you could use, but the WinForms timer has the advantage of firing in the UI thread so you don't need to worry about it. One thing to note is that you need to think about what happens if the mouse hover event fires again before the timer expires.

Finally, if you are using WPF instead of WinForms, you could probably do the whole thing in XAML using animation.

Upvotes: 1

Related Questions