Missing UI Control on tab, why calling HandleNeeded fixes it?

故事扮演 提交于 2020-03-06 05:57:08

问题


I had one UI object attached to a tab that will go missing only under 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?


回答1:


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.




回答2:


If the control is created during runtime: make sure the parent and owner of the component are set!



来源:https://stackoverflow.com/questions/2070084/missing-ui-control-on-tab-why-calling-handleneeded-fixes-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!