79729404

Date: 2025-08-08 06:38:09
Score: 1.5
Natty:
Report link

Thank you @Jimi to mention the root of problem. As you said, Handle of control was not created. DGV has a LoadingScreen when user wants to assign value as DataSource but, this screen is a form and must cover entire area of DGV. Meanwhile, this screen is visible in front of other controls and since the actual size and position of hidden DGV is not accessible, finally LoadingScreen is displayed in wrong size and position.

Solution

Inside code lines where the LoadingScreen must be shown, IsVisible method can return actual situation to decide possibility of showing LoadingSreen. As you can see in the following code, two factor for this purpose is checked: 1) IsHandleCreated (As you mentioned) 2) DGV is visible on screen.

public static Form Create(
    Control control,
    bool coverParentContainer = true,
    bool coverParentForm = false,
    string title = "Loading...",
    double opacity = 0.5)
{

    var frm = new CesLoadScreen();
    frm._title = title;
    frm.Opacity = opacity;

    if (!IsVisible(control))
        return frm;

    SetLoadingScreenSize(frm, coverParentContainer, coverParentForm, control);

    control.Resize += (s, e) 
                => SetLoadingScreenSize(frm, coverParentContainer, coverParentForm, control);

    frm.Show(control.FindForm());
    Application.DoEvents();

    return frm;
}

public static bool IsVisible(Control control)
{
    Rectangle screenBounds = Screen.FromControl(control).Bounds;
    Rectangle controlBounds = control.RectangleToScreen(control.ClientRectangle);

    bool isOnScreen = screenBounds.IntersectsWith(controlBounds);

    if (!control.IsHandleCreated || !isOnScreen)
        return false;

    if (!control.Visible)
        return false;

    return true;
}
Reasons:
  • Blacklisted phrase (0.5): Thank you
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Jimi
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Hadi Mazareei