Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bring Winforms control to front

Tags:

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(); } 
like image 640
Nathan Avatar asked Apr 11 '10 17:04

Nathan


People also ask

How can I bring my application window to the front?

"Activate" does make Windows bringing current form to front of any other application currently open within the O/S. On the other hand, "bring to front" only make desired form showing in front of other forms within current application (not O/S). Form.

How do I make a Windows form always on top?

To make the active window always on top, press Ctrl + Spacebar (or the keyboard shortcut you assigned). Press the keyboard shortcut again to disable “always on top” for the active window. For script options, right-click on the AutoHotkey icon in the system tray.

How do I turn off WinForms full screen?

The only thing you can do is completely disable resizing your window. In WinForms, you do that by setting the FormBorderStyle property to one of the following: FormBorderStyle. FixedSingle , FormBorderStyle. Fixed3D , or FormBorderStyle.


2 Answers

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.

like image 157
Hans Passant Avatar answered Sep 25 '22 21:09

Hans Passant


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.

like image 39
SLaks Avatar answered Sep 22 '22 21:09

SLaks