I have created a Form
with a loading Panel
on it. The idea being that I want to show this loading panel until my dynamically created control is ready to display.
The loading Panel
is visible initially, and then during the OnShown
event I am creating my control and adding it to the page. The reason I am using OnShown
is that the Form
is being used in an Mdi scenario so I need it to be fully displayed before I start loading the control (if I try this in the Load event then the Mdi Tab doesn't display until my control is loaded).
The problem is that there is noticable flicker which I believe is due to the fact that when I add my control to the Controls
collection:
a) the Visible property is immediately set to true. b) my control seems to appear in front of the loading Panel even though the z-index appears to be correct.
Here is the nub of the issue
protected override void OnShown(EventArgs e)
{
Debug.WriteLine(loadingPanel.Visible); //true
Debug.WriteLine(Controls.GetChildIndex(loadingPanel)); //0
Debug.WriteLine(myControl.Visible); //false
myControl.Visible = false;
Controls.Add(myControl);
//**
Debug.WriteLine(myControl.Visible); //true
Debug.WriteLine(Controls.GetChildIndex(loadingPanel)); //0
Debug.WriteLine(Controls.GetChildIndex(myControl)); //1
Debug.WriteLine(loadingPanel.Visible); //true
base.OnShown(e);
}
I was hoping that I could add my control to the collection and it would remain Visible = false
so that I could set Visible = true
when my control's Load event had completed. Instead, my control comes into view and I get flickering. Interestingly, if I don't set loadingPanel.Visible = false
at any point then once my control has finished loading, the loadingPanel re-appears and hides my control.
Any ideas?
Somewhat strangely, this is how I managed to solve my issue:
I can only assume that when adding a control to the Control collection then if the handle is not created it will auto set it to Visible. By making it visible before adding it to the collection, the relevant handles are created by do not affect the parent control. Then when it is added to the collection later on, it still does not interfere with the parent control.