How do you check whether a userControl is in front of others? Is there easy way to do that? I use bringToFront method when my user control was clicked, but now I need to determine if it is in front at the moment.
If you just want to know which control is at the front of a parent collection, just do the following:
private bool IsControlAtFront(Control control)
{
return control.Parent.Controls.GetChildIndex(control) == 0;
}
Notice that the Z-Index 0 is the top most control, the higher the number, the lower down the hierarchy it is.
Also, this code above will currently only work for a Control within an individual parent. It will also need to recursively check the parent is at z-index 0 as well.
This will work for any Control anywhere within 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With