24

Are there any other methods of bringing a control to the front other than control.BringToFront()?

I have series of labels on a user control and when I try to bring one of them to front it is not working. I have even looped through all the controls and sent them all the back except for the one I am interested in and it doesn't change a thing.

Here is the method where a label is added to the user control

private void AddUserLabel()
{
    var field = new UserLabel();

    userContainer.Controls.Add(field);
    SendLabelsToBack(); // Send All labels to back

    userContainer.Controls[field.FieldName].BringToFront();
}

Here is the method that sends all of them to the back.

private void SendLabelsToBack()
{
    foreach (var label in userContainer.Controls);
        label.SendToBack();
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Nathan
  • 5,059
  • 16
  • 48
  • 61
  • Some example code would be nice. Are you sure the controls are in the same container control. Setting a control to front will only make it the front control in its container. – Patrick Apr 11 '10 at 18:05
  • This post helped me solve the exact same problem. – IbrarMumtaz Dec 26 '11 at 15:20

5 Answers5

27

Yeah, there's another way. The Controls.SetChildIndex() also changes Z-order. The one with index 0 is the one on top. Doesn't buy you anything though, BringToFront() uses this method.

Your SendLabelsToBack() method as given cannot work, it will also send the label to added to the back. But your next statement fixes that again.

Okay, that doesn't work, which means the BringToFront() method doesn't get executed. Look in the Output window for a "first chance exception" notification. As written, your SendLabelsToBack() will cause an exception if the user control contains any control other than a UserLabel. Also, set a breakpoint after the BringToFront() call and check the value of userContainer.Controls[0].Name when it breaks.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I don't really follow this the last portion because the form is only instantiated one time. – Nathan Apr 11 '10 at 18:53
  • Just trying to come up with a reason that the label would not be visible. Is it in fact visible but positioned wrong? Or is it not visible at all? – Hans Passant Apr 11 '10 at 18:57
  • It is visible behind another label. – Nathan Apr 11 '10 at 18:58
  • After rechecking the name of the label I realized another label had accidentally been given the same name. So its BringToFont was being called instead of the one I wanted. Should have caught this before asking the question...Thanks Hans. – Nathan Apr 11 '10 at 20:08
6

Controls' z-index is per-container.

If you call BringToFront on a control that is inside a container (such as a Panel), it will not bring the container to the front.
Therefore, the control will only go in front of other controls in that container.

To see what containers your controls are in, you can use the Document Outline pane in the View menu.

EDIT: Your userContainer control is probably behind a different control.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • This is the "Gotcha"! Try moving the control you want to change visibility of out of the container, eg move a `Label` control out of a `GroupBox` container parent to be a child of the main `Form` container and it'll work. – SteveCinq Aug 29 '18 at 01:38
5

Have you tried Invalidate() after BringToFront()? BringToFront does not raise the Paint event

try this:

private void SendLabelsToBack()
{
    foreach (var label in userContainer.Controls)
    {
        label.SendToBack();
        label.Invalidate();
    }
}
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Patrick
  • 17,669
  • 6
  • 70
  • 85
  • 1
    I just tried and that didn't help either. It appears like a couple labels have the same z-index and some don't. From my understanding they should all have the same z-index if I set them manually. – Nathan Apr 11 '10 at 18:31
  • Perfect answer. Worked for me! – Ashton Sep 19 '16 at 17:01
3

I think you just need to change your last line:

userContainer.Controls[field.FieldName].BringToFront(); 

to this:

userContainer.Controls[field.Name].BringToFront(); 

When you use a string as the indexer for the Controls collection, it goes by the Name property of the control (not the FieldName property).

Since you're just trying to bring the most recently-added control to the top, this would also work:

userContainer.Controls[userContainer.Controls.Count - 1].BringToFront();
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
  • No; otherwise, he would get an exception. – SLaks Apr 11 '10 at 18:33
  • @Slaks: based on the usage in the question, it looks like `FieldName` would return the same value no matter which instance, like `FieldTop` and `FieldLeft` etc. So `FieldName` may just happen to be the same name as the first instance, so the call to BringToFront is always applied to the first control. – MusiGenesis Apr 11 '10 at 18:37
  • Why not type just `field.BringToFront()`? There's really no need to find it in the control collection when he already has a reference to it. – Patrick Apr 11 '10 at 18:42
  • FieldName is property I created for the label(which is a user control as well) – Nathan Apr 11 '10 at 18:42
0

From my experience looks like windows puts all controls belonging to one graphic container(pane, group box...etc) in a software collection. The collection is ordered by child index which is a property of every control in that container. The trick is that children with the same index can and do exists. In this case windows will paint those children ordered relative to others but between them it will paint them in the reverse order they had been added to the container.

Long story short: for one container-you need to make sure controls have different indexes by changing ALL NOT just SOME of the indexes when you want to change the z-order. For example:

foreach (Control newControl in TopControl.Controls)
{
   TopControl.Controls.SetChildIndex(newControl,indexlogic(newControl));
}

where indexLogic(newControl ) is your method of calculation of the index of particular control.

Gogu CelMare
  • 699
  • 6
  • 15