Make WinForm control appear on top of all other controls

tentmaking picture tentmaking · Aug 23, 2012 · Viewed 7.4k times · Source

I have a WinFrom, in which I am hiding all borders and the Control Box. Inside the WinForm I have a RECT() (not a WinFrom control) the RECT fills the entire WinForm.

I am trying to add a Label to the WinForm, but I want the label to appear on top of the RECT. The Label appears on the WinForm, but never on top of the RECT. I've tried using the following:

/*App Runs - Label does not show up*/
_label.Text = "This is the label";
_label.BackColor = Color.Cornsilk;
_form.Controls.Add(_label);
_form.Controls.SetChildIndex(_label, 0);

/*App Does Not Run*/
_label.Text = "This is the label";
_label.BackColor = Color.Cornsilk;
_form.Controls.SetChildIndex(_label, 0); //trying to set the index before I add the label to the form
_form.Controls.Add(_label);

/*App Runs - Label does not show up*/
_label.Text = "This is the label";
_label.BackColor = Color.Cornsilk;
_label.BringToFront();
_form.Controls.Add(_label);

/*App Runs - Label does not show up*/
_label.Text = "This is the label";
_label.BackColor = Color.Cornsilk;
_form.Controls.Add(_label);
_label.BringToFront();

As you can see, I've tried a lot of different stuff and nothing is working. I have also tried adding the label after the RECT has been added, to no avail. I am having a similar issue with adding a Background Image (though not the question being asked here). Does anyone know of a more forceful way to make the Label appear on top of the RECT?

Also, because of the API and dll's I am using, I cannot use something other than a RECT or WinForms.

Answer

Reed Copsey picture Reed Copsey · Aug 23, 2012

You can use BringToFront on the label itself:

_label.BringToFront();

This will bring the label to the front of the Z order on the form, so it should display on top of other form elements.

I have a RECT() (not a WinFrom control) the RECT fills the entire WinForm

A "RECT" isn't a control - it's a definition size and position. Depending on what you're using to display your background, this may not work. If it's painting into the entire form, it could be overwriting your other controls, and "masking" them, no matter what you use for z order. Without more information, it could be difficult to provide guidance, but you'd have to make sure you cause the label to redraw after the "RECT".