Missing UI Control on tab, why calling HandleNeeded fixes it?
I had one UI object attached to a tab that will go missing only und开发者_如何学编程er a certain sequence of interaction with the program, and if it's missing, closing the program will result in AV and error of Invalid Window Handle. As I was trying to find out why, I found that calling its .Handle or .HandleNeeded at the start will fix the problem. But that doesn't answer why does it fix the problem.
So I would hope to know that is this common? Has anyone encountered this strange bug before? And know the reason?
If you're using a TabControl, Delphi only creates the controls for each page when the page is needed (for speed and resource usage reasons). Trying to access controls on a page that hasn't been shown will cause issues, unless you call .HandleNeeded. The call to .HandleNeeded tells Delphi you need the handle now, and eliminates the problem.
It's not a bug, BTW. It's an intentional design decision, for speed and minimization of resource usage as I mentioned above.
If the control is created during runtime: make sure the parent and owner of the component are set!
精彩评论