Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#在鼠标移动到其他面板上后,使面板在特定时间内可见_C#_Mousehover - Fatal编程技术网

C#在鼠标移动到其他面板上后,使面板在特定时间内可见

C#在鼠标移动到其他面板上后,使面板在特定时间内可见,c#,mousehover,C#,Mousehover,我需要使面板在鼠标移动到其他面板上后的特定时间内可见 我已经解决这个问题很久了。我试着用定时器,但没有成功 这是我的代码: this.MouseHover += new EventHandler(myMouseHover); [...] //event handler private void myMouseHover(object sender, EventArgs e) { this.prevPanel.Visible

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

这是我的代码:

 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;

        }

您可以使用
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;
}

根据您使用的计时器类型,您需要输入代码以隐藏面板以响应滴答声事件

例如,使用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中完成整个工作。

谢谢您,但我的问题是我需要在mouseHover处理程序中完成它,所以在mouseHover操作之后,我很乐意帮助您,您可以在事件中添加Timer.Start方法以引发此代码生成InvalidOperationException。不要使用System.Timers.Timer,其已用事件在线程池线程上运行。您无法从这样的线程更新UI。@AghilasYakoub:Timer.Tick not Timer.Ellapsed。因为一致的命名太容易了!如果你把鼠标移到面板上,然后向外,然后再向后,如此类推,你将创建很多计时器。这可能是不可取的。现在,您正在将方法附加到EverHouse over上的
已用
处理程序,因此当时间用完时,代码将被触发数次。您应该设置间隔并在构造函数中附加处理程序。
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;
}