Question

We have some legacy delphi custom components that override createwnd. These components sit on a panel within a form.

When the form and panel get instantiated, so do the components, and createwnd is called once. This makes sense.

I was surprised to discover that createwnd will gets called again however, if I switch the parent of the panel during runtime.

Eg, under certain circumstances I might change the parent container of Panel1 to be Form2.panel2. Basically drawing Panel1 inside Form2's panel at runtime.

TForm1.buttonclick(..)
begin
..
  Panel1.parent := Form2.panel2;
..
end;

Why are the custom components in Panel1 being recreated, when the parent changes?

And is the behavior the same for all components in Panel1, even ones that are not custom, for example if I also had a TButton on Panel1, would that also get recreated when parents are switched? I don't think it does, which is why I was so surprised.

Finally, if this behavior is incorrect, how do I make it so createwnd doesn't get called when parent containers change.

Thank you!

Was it helpful?

Solution

All controls within the panel that descend from TWinControl have their window handles recreated, as well as the panel's window handle (The reason is the other way around).

If your code has to be called only once per program's life-time, then CreateWnd is not the right place. Try the constructor instead.

Furthermore, replacing a panel from one form to another could mix up ownership of the panel, and could result in unexpected behaviour or even errors. See: How to detach a panel and show it in a separate window?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top