开发者

What determines which control is drawn in front of which in C#? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

开发者_开发问答I think the question is clear enough...


Assuming winforms:

  1. The order they appear in their parent container's controls collection. For controls created by the designer, this is usually the order in which they were first created. However, I think certain events (ie moving a control in or out of a group box) can cause a control to be re-written to the bottom of the file when code for the designer file is re-generated.
  2. Calls to .BringToFont(), .SendToBack(), or .SetChildIndex() (on the parent)


If this is for windows forms, you can control the z-order using the SetChildIndex method on the form's Controls collection.

When SetChildIndex is called, the Control referred to by the child parameter is moved to the position specified by newIndex and the other Control references in the Control.ControlCollection are reordered to accommodate the move. The control with an index value of zero is at the top of the z-order, and higher numbers are closer to the bottom.

Controls with a lower index will appear in front of those with higher indexes.


Technically, their Z-index determines it. Most of the time the controls all have the same z-index by default and they will then revert to the order they were added to the container. But of course there's many caveats to that too such as airspace issues in WPF/Silverlight/HTML hosting HWND's or adorner layers which are always on top, etc.

(Accidentally brain-farted and posted this as a comment to Joel's answer at first...)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜