我需要在鼠标移动到其他面板后让面板在特定时间内可见 我一直在解决这个问题。我试过使用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;
}
答案 0 :(得分: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;
}
答案 1 :(得分:1)
根据您使用的计时器类型,您需要放置隐藏面板的代码以响应tick事件。
例如,使用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中完成所有操作。
答案 2 :(得分: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);
}