我正在尝试使用c#中的x时间使表单不可见。 有什么想法吗?
谢谢, 乔恩
答案 0 :(得分:16)
BFree在我测试时发布了类似的代码,但这是我的尝试:
this.Hide();
var t = new System.Windows.Forms.Timer
{
Interval = 3000 // however long you want to hide for
};
t.Tick += (x, y) => { t.Enabled = false; this.Show(); };
t.Enabled = true;
答案 1 :(得分:8)
快速而肮脏的解决方案利用封闭。无需定时器!
private void Invisibilize(TimeSpan Duration)
{
(new System.Threading.Thread(() => {
this.Invoke(new MethodInvoker(this.Hide));
System.Threading.Thread.Sleep(Duration);
this.Invoke(new MethodInvoker(this.Show));
})).Start();
}
示例:
//使表单隐藏5秒
Invisibilize(new TimeSpan(0,0,5));
答案 2 :(得分:3)
在班级做同样的事情:
Timer timer = new Timer();
private int counter = 0;
在构造函数中执行以下操作:
public Form1()
{
InitializeComponent();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
}
然后你的事件处理程序:
void timer_Tick(object sender, EventArgs e)
{
counter++;
if (counter == 5) //or whatever amount of time you want it to be invisible
{
this.Visible = true;
timer.Stop();
counter = 0;
}
}
然后,无论你想让它变得不可见(我将在点击按钮时演示):
private void button2_Click(object sender, EventArgs e)
{
this.Visible = false;
timer.Start();
}
答案 3 :(得分:1)
请记住,有几种类型的计时器可用: http://msdn.microsoft.com/en-us/magazine/cc164015.aspx
并且不要忘记在处理程序的持续时间内禁用计时器,以免打断你的自我。相当尴尬。