Show certain time window form

2

Good I'm doing in window form a mini system that when there are 2 buttons

1: first button called a raffle is to appear at a certain time and my 8 numbers appear label 2: regenerate button is only bleach is to say that they are visible again

but my problem is that when I first put the draw button shows correctly a certain time,

Now when he finishes and I put the second button and then I press the first button, he shows me suddenly.

This is the part of the window form view

this is my code for the first button:

 private void button3_Click(object sender, EventArgs e)
        {
 SetInterval(1000, (obj, ev) =>
            {
                if (!lbln1.Visible)
                {
                    lbln1.Show();
                }
                else if (!lbln2.Visible)
                {
                    lbln2.Show();
                }
                else if (!lbln3.Visible)
                {
                    lbln3.Show();
                }
                else if (!lbln4.Visible)
                {
                    lbln4.Show();
                }
                else if (!lbln5.Visible)
                {
                    lbln5.Show();
                }

                else if (!lbln6.Visible)
                {
                    lbln6.Show();
                }
                else if (!lbln7.Visible)
                {
                    lbln7.Show();
                }
                else if (!lbln8.Visible)
                {
                    lbln8.Show();
                }
                else if (!label1.Visible)
                {
                    label1.Show();
                }

                else if (!lblcliente.Visible)
                {
                    lblcliente.Show();
                }
                else if (!lbloficina.Visible)
                {
                    lbloficina.Show();
                }
                else if (!lblfelicitaciones.Visible)
                {
                    lblfelicitaciones.Show();
                }
                else if (!lbldireccion.Visible)
                {
                    lbldireccion.Show();
                }

            });
}

Second Button:

private void button1_Click(object sender, EventArgs e)
        {

             DialogResult res = MessageBox.Show("Desea Sortear Nuevamente?", "Aviso del sistema",
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Question);
             if (res == DialogResult.Yes)
             {lbln1.Text = "";
                 lbln2.Text = "";
                 lbln3.Text = "";
                 lbln4.Text = "";
                 lbln5.Text = "";
                 lbln6.Text = "";
                 lbln7.Text = "";
                 lbln8.Text = "";
                 lblcliente.Text = "";
                 lblfelicitaciones.Text = "";
                 lblnoesganador.Text = "";
                 lbloficina.Text = "";
                 lbldireccion.Text = "";
                 label1.Text = "";

                 MessageBox.Show("Se regenero el sorteo correctamente", "Aviso del Sistema");
                 button2.Enabled = false;

             }
        }

this is my void SetInterval

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;

            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();

        }

    
asked by PieroDev 07.07.2018 в 00:18
source

1 answer

2

You are complicating something, that in winforms is done with the controls of the form itself.

Add in the form 6 labels, 2 buttons and a TIMER control.

Leave the default names, except for the timer , change it to tmrMostrar and put the property interval in 1000.

You will have to connect the events that I describe below to your controls. There is an event for the first button, another for the second and a tick event for the timer.

The simplified code with explanations in the comments is:

//Lista de los labels a ir mostrando
private List<Label> labels = new List<Label>();
//Siguiente label a mostrar
private int LabelAMostrar = 1;
//El valor del ultimo label, o la cantidad
private int MaximoLabel = 6;

public Form1()
{
    InitializeComponent();
    //Armo un array de controles, se puede recorrer el form, pero asi es mas rapido
    labels.Add(label1);
    labels.Add(label2);
    labels.Add(label3);
    labels.Add(label4);
    labels.Add(label5);
    labels.Add(label6);
    cambiarEstadoLabels(false);
}

private void button1_Click(object sender, EventArgs e)
{
    //Empezar el timer
    tmrMostrar.Start();
    //apagar el boton
    button1.Visible = false;
}

private void cambiarEstadoLabels(bool estado)
{
    foreach (Label l in labels)
    {
        l.Visible = estado;
    }
}

private void tmrMostrar_Tick(object sender, EventArgs e)
{
    //Esto se podria hacer mucho mejor con linq
    foreach (Label l in labels)
    {
        if (l.Name == $"label{LabelAMostrar}")
        {
            l.Visible = true;
            LabelAMostrar += 1;
            if (LabelAMostrar > MaximoLabel)
            {
                //apago el timer
                tmrMostrar.Stop();
                //Muestro el boton de reseteo
                button2.Visible = true;
            }
            //si llegue aca, es que ya encontre el label a mostrar
            return;
        }
    }
}

private void button2_Click(object sender, EventArgs e)
{
    cambiarEstadoLabels(false);
    button2.Visible = false;
    button1.Visible = true;
    LabelAMostrar = 1;
}

This code will show a label per second, until the last one.

    
answered by 07.07.2018 / 02:34
source