Pregunta
Estoy intentando hacer que un formulario sea invisible durante x cantidad de tiempo en c #. ¿Alguna idea?
Gracias, Jon
Solución
BFree ha publicado un código similar en el tiempo que me tomó probar esto, pero aquí está mi intento:
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;
Otros consejos
Solución rápida y sucia aprovechando los cierres. No se requiere temporizador!
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();
}
Ejemplo:
// Hace que la forma sea invisible durante 5 segundos
Invisibilizar (nuevo TimeSpan (0, 0, 5))
En el nivel de clase, haz algo como esto:
Timer timer = new Timer();
private int counter = 0;
En el constructor haz esto:
public Form1()
{
InitializeComponent();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
}
A continuación, su controlador 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;
}
}
Luego, donde sea que desee que sea invisible (lo demostraré aquí con un botón):
private void button2_Click(object sender, EventArgs e)
{
this.Visible = false;
timer.Start();
}
Tenga en cuenta que hay varios tipos de temporizadores disponibles: http://msdn.microsoft.com/en-us/magazine/cc164015. aspx
Y no olvides desactivar el temporizador durante la duración del controlador, para que no te interrumpas a ti mismo. Bastante embarazoso.