7

我正在尝试在 c# 中使表单在 x 时间内不可见。有任何想法吗?

谢谢,乔恩

4

4 回答 4

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;
于 2009-01-05T01:04:53.400 回答
8

利用闭包的快速而肮脏的解决方案。不需要Timer

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();
    }

例子:

// Makes form invisible for 5 seconds.

Invisibilize(new TimeSpan(0, 0, 5));
于 2009-01-05T01:40:20.457 回答
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();
        }
于 2009-01-05T01:03:00.127 回答
1

请记住,有几种类型的计时器可用:http: //msdn.microsoft.com/en-us/magazine/cc164015.aspx

并且不要忘记在处理程序期间禁用计时器,以免打扰自己。比较尴尬。

于 2009-01-05T01:28:20.840 回答