UserControl bring to the front (have the focus)

1

I am working on a desktop application (Windows forms) in which my windows are UserControl which I call from a menu that is vertically on the left side, on the right side I have a Panel that serves as a floor where I call the UserControl, but I just realized that when you have a UserControl already in the Panel and you call another UserControl after coming over the one that was already underneath.

Is there any way to bring a UserControl to the front if there is already one inside a Panel?

    
asked by Pedro Ávila 29.04.2016 в 20:58
source

2 answers

1

If you are loading User control within a panel it would be advisable to download the one you had previously removed it

public void mnuxx_Click(..){

   Panel1.Controls.Clear();

   UserControl1 uc = new UserControl1();
   Panel1.Controls.Add(uc);

}

This way you would not have problems of superposition of controls, also if you are only having an active control so that you have the rest in memory.

    
answered by 29.04.2016 / 21:04
source
2

What happens is that when you add a Control to the collection controls of each container the z-order is updated and it is sent to the background. The good thing is that you have available methods to modify this.

You have this method available when inheriting from Control:

Control.BringToFront()

For example:

 panel1.BringToFront();

You can also manage the z-order of each control within the collection Controls of each container with GetChildIndex and SetChildIndex

// Obtener el  index de un control
int zIndex = controlPadre.Controls.GetChildIndex(textBox);
// Enviar al frente
textBox.BringToFront();
// aqui tu programa hace algo ...
// Luego lo mandamos donde estaba antes de traerlo al frente
controlPadre.Controls.SetChildIndex(textBox, zIndex);
    
answered by 29.04.2016 в 21:11