質問
C#でx時間の間フォームを非表示にしようとしています。 アイデアはありますか?
ありがとう、 ジョン
解決
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;
他のヒント
クロージャーを利用した迅速でダーティなソリューション。タイマーは必要ありません!
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));
クラスレベルで次のようにします:
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();
}
利用可能なタイマーにはいくつかのタイプがあることに留意してください。 http://msdn.microsoft.com/en-us/magazine/cc164015。 aspx
そして、ハンドラーの実行中はタイマーを無効にすることを忘れないでください。むしろ恥ずかしい。
所属していません StackOverflow