How to check if userControl is before others in C #?

How do you check if user control is in front of others? Is there an easy way to do this? I use the bringToFront method when my user control was clicked, but now I need to determine if it is in front at the moment.

+6
c # winforms
source share
1 answer

If you just want to know which control is in front of the parent collection, follow these steps:

private bool IsControlAtFront(Control control) { return control.Parent.Controls.GetChildIndex(control) == 0; } 

Note that Z-Index 0 is the topmost one , the larger the number lower down the hierarchy.

In addition, this code above currently only works for a control inside an individual parent. You will also need to recursively verify that the parent is also at z-index 0.

This will work for any control anywhere on the form:

 private bool IsControlAtFront(Control control) { while (control.Parent != null) { if (control.Parent.Controls.GetChildIndex(control) == 0) { control = control.Parent; if (control.Parent == null) { return true; } } else { return false; } } return false; } 
+7
source share

All Articles