I have run across a strange piece of code inside a Control.
if (ParentForm != null)
{
traceBlock.Log("ParentForm is null");
if (!ParentForm.IsHandleCreated)
{
ParentForm.HandleCreated += (sender, e) =>
{
var text = PhysDocContext.Document.GetHeader(PhysDocContext);
ParentForm.Text = text;
};
}
}
This code fires a few times with ParentForm == null. It’s obvious the logger is logging inaccurate information. What really strikes me as odd is checking for IsHandleCreated. In my mind this code will never fire. So the event won’t hook and the header will never be set.
What’s even more odd is that the ParentForm is set to a Panel control owned by the parent form. It is also set by the Form that is the parent. Leading me to believe this is a more impossible situation.
Is it normal (possible even) for a Form to be accessible in .NET that has no handle?
Yes, because a Form (or a Control for that matter) is merely a wrapper class around a few Win32 API calls. The code you use to create a form is used to create the .NET object.
Most likely only when the form is shown, the calls to Win32 are made, finally giving the control a handle.