Bring Winforms control to front
Posted
by Nathan
on Stack Overflow
See other posts from Stack Overflow
or by Nathan
Published on 2010-04-11T17:59:36Z
Indexed on
2010/04/11
19:53 UTC
Read the original article
Hit count: 531
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()
{
UserLabel field = new UserLabel();
++fieldNumber;
field.Name = "field" + fieldNumber.ToString();
field.Top = field.FieldTop + fieldNumber;
field.Left = field.FieldLeft + fieldNumber;
field.Height = field.FieldHeight;
field.Width = field.FieldWidth;
field.RotationAngle = field.FieldRotation;
field.Barcode = field.BarCoded;
field.HumanReadable = field.HumanReadable;
field.Text = field.FieldText;
field.ForeColor = Color.Black;
field.MouseDown += new MouseEventHandler(label_MouseDown);
field.MouseUp += new MouseEventHandler(label_MouseUp);
field.MouseMove += new MouseEventHandler(label_MouseMove);
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 (UserLabel lbl in userContainer.Controls)
{
lbl.SendToBack();
}
}
© Stack Overflow or respective owner