dimanche 28 juin 2015

Why a Panel blinks while removing it and its childs?

I have a form with a custom controls, UserControls. One of these controls is composed with other controls: TableLayoutPanel, PictureBox (it is inside of another UserControl), Label. Visually they are depicted in the following way:

Visual description

As we can see in the image, the red rectangle is a UserControl, the orange rectangles are TableLayoutPanel, the yellow and green chairs are other UserControl controls composed by a PictureBox and a Label.

The chairs (yellow and green ones) are draw dynamically. For example to draw the yellow chairs:

    private void DibujarSillasEconomicas()
    {
        Silla[] cheapChairs = m_avion.GetCheapChairs();
        Silla silla;
        byte fila_silla = 0;
        byte col_silla = 0;
        ControlVisualChair ctlSillaGrafica;

        for(int num_silla = 0; num_silla < cheapChairs.Length; ++num_silla)
        {
            silla = cheapChairs[num_silla];
            ctlSillaGrafica = new ControlSillaGrafica(silla);
            ctlSillaGrafica.Dock = DockStyle.Fill;
            ctlSillaGrafica.BackColor = Color.Black;

            if (num_silla > 0 & num_silla % 6 == 0)
            {
                ++fila_silla;
                col_silla = 0;
            }

            tplSillasEconomicas.Controls.Add(ctlSillaGrafica, col_silla == 3? ++col_silla : col_silla, fila_silla);

            ++col_silla;
        }
    }

These chairs and the yellow ones are drawn correctly. The problem appears when I want to register a passenger:

Adding a passenger

Note that when I add a passenger the controls blink. In code this what I do when I finish adding a passenger:

this.Controls.Remove(ctlAvion); // Removes the actual UserControl (red rectangle)
ctlAvion = new ControlAvion(m_avion); // Creates a new one
ctlAvion.Location = new Point(2, 13);
ctlAvion.Size = new Size(597, 475);
this.Controls.Add(ctlAvion); // Adds the new UserControl to the main controls (a Form).

How I can avoid this blink effect when?

I have tried the following UserControls methods:

ctlAvion.Invalidate();
ctlAvion.Update();
ctlAvion.Refresh();

but they does not work!

Thanks in advance for your help!

Aucun commentaire:

Enregistrer un commentaire