Show of a certain time window form c #

0

Good I'm doing in window form a module where I have 4 label and a button. At first those label will be hidden and when you press the button the first label appears a certain time once the second label appears also appear a certain time and successively. I realized it in webform.aspnet I did it with javascript. But in window form they do not use javascript. I wish they could help me. I attached the image so that they understand me better.

    
asked by PieroDev 12.06.2018 в 13:49
source

1 answer

5

Look, create a function called setInterval to pass the time I want it to take for each label to appear and a callback with what I want to run in that time.

private void SetInterval(int interval, EventHandler callback)
{
    //Creo el temporizador
    Timer timer = new Timer();
    //Le establezco el intervalo en el que va a repetir la operación
    timer.Interval = interval;
    //Asigno que se ejecutara al llegar el tiempo estimado
    timer.Tick += (obj, ev) =>
    {
        //En este caso ejecuto el callback que paso como parametro
        callback(obj, ev);
    };
    //Inicializo el temporizador
    timer.Start();
}

Now in the onClick event of the button I call setInterval and I check if the buttons are visible and I show them progressively, I have two ways one that is difficult to maintain if you are going to increase the labels to be displayed and another one that is more optimal , I'm going to put both of you and you decide which one to use

EASY:

SetInterval(1000, (obj, ev) =>
{
    if (!label1.Visible)
    {
        label1.Show();
    }
    else if (!label2.Visible)
    {
        label2.Show();
    }
    else if (!label3.Visible)
    {
        label3.Show();
    }
    else if(!label4.Visible)
    {
        label4.Show();
    }       
});

MORE OPTIMAL:

//Aqui guardo todos los labels que quiero mostrar progresivamente ordenados por el orden de aparición
Control[] labels = new Control[] { label1, label2, label3, label4 };
//Guardo la posición en la que debo comenzar en el Tag del botón
button1.Tag = 0;
//Llamo a la función creada
SetInterval(1000, (obj, ev) =>
{       
    //Obtengo la posición actual sobre la cual voy a trabajar
    int pos = Convert.ToInt32(button1.Tag);
    //Si no estoy fuera de los limites del arreglo de labels y el label esta oculto...
    if (pos < labels.Length && !labels[pos].Visible)
    {
        //Muestro el label correspondiente por el orden
        labels[pos++].Show();
        //Actualizo la posición en la que se quedo del arreglo de labels
        button1.Tag = pos;
    } 
});

I hope I have served you.

    
answered by 12.06.2018 / 14:15
source