Pergunta
Eu estou tentando fazer uma forma invisível para x quantidade de tempo em c #. Alguma idéia?
Obrigado, Jon
Solução
BFree postou código semelhante no tempo que me levou para testar isso, mas aqui é a minha tentativa:
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;
Outras dicas
Rápido e solução suja aproveitando encerramentos. No Temporizador Obrigatório!
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();
}
Exemplo:
// Makes formar invisível por 5 segundos
Invisibilize (new TimeSpan (0, 0, 5));
Ao nível da classe fazer algo como isto:
Timer timer = new Timer();
private int counter = 0;
No construtor de fazer isso:
public Form1()
{
InitializeComponent();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
}
Em seguida, o manipulador de eventos:
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;
}
}
Então onde quer que você deseja torná-lo invisível (vou demonstrar aqui em um clique de botão):
private void button2_Click(object sender, EventArgs e)
{
this.Visible = false;
timer.Start();
}
Tenha em mente que existem vários tipos de temporizadores disponíveis: http://msdn.microsoft.com/en-us/magazine/cc164015. aspx
E não se esqueça de desativar o temporizador para a duração do manipulador, para que não interrompa o seu self. Bastante embaraçoso.