0

在鼠标移到其他面板上后,我需要让面板在特定时间内可见,我已经解决这个问题很长时间了。我尝试使用 Timer,但没有成功。

这是我的代码:

 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;

        }
4

3 回答 3

1

您可以使用Timer和定义Interval属性

财产 :

private System.Windows.Forms.Timer aTimer;

在里面 :

    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;

代表 :

private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
      this.prevPanel.Visible = false;
      this.nextPanel.Visible = false;
}
于 2012-09-26T13:16:09.857 回答
1

根据您使用的计时器类型,您需要放置将隐藏面板以响应滴答事件的代码。

例如,使用 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;
}

您可以使用其他计时器,但 WinForms 计时器具有在 UI 线程中触发的优势,因此您无需担心。需要注意的一件事是,您需要考虑如果鼠标悬停事件在计时器到期之前再次触发会发生什么。

最后,如果您使用 WPF 而不是 WinForms,您可能可以使用动画在 XAML 中完成所有工作。

于 2012-09-26T13:30:33.697 回答
0
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);
}
于 2012-09-26T13:29:48.803 回答