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 に答える