Send controls to the back or front of the stacking order in C#

A control's SendToBack and BringToFront methods move a control to the back or front of the stacking order. The buttons contained in this program use the following code to move themselves to the front or back of the stacking order.

private void btnToTop_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
btn.BringToFront();
}

private void btnToBottom_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
btn.SendToBack();
}

You can also use the control's parent's Controls collection's SetChildIndex to change the control's position in the collection. Set the index to 0 for the front of the stacking order. Set it to Controls.Count - 1 for the back of the stacking order.

For example, the following code moves the button btn so it is second from the front.

btn.Parent.Controls.SetChildIndex(btn, 1);

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.