开发者

How to check if a userControl is in front of others in C#?

开发者 https://www.devze.com 2023-01-08 18:39 出处:网络
How do you check whether a userControl is in fro开发者_开发知识库nt of others? Is there easy way to do that?

How do you check whether a userControl is in fro开发者_开发知识库nt 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;
}
0

精彩评论

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