为 Winforms Control 添加 Load 事件,就像 Form 类一样
有没有办法像 System.Windows.Forms.Form.Load 一样获取 System.Windows.Forms.Control 的 Load 事件?
我想在控件首次显示之前运行一些初始化代码。
另外,如果能够对 System.Windows.Forms.ToolStripStatusLabel 执行相同的操作,那就太好了,它实际上不是一个控件,但工作起来却像一个控件。
理想情况下,我可以这样做: control.OnLoad(() => { 在这里做一些事情; });
其中 OnLoad 是一个扩展方法,当“控件”“加载”时,它将运行参数 Action。
谢谢!
is there a way I can get a Load event for System.Windows.Forms.Control just like System.Windows.Forms.Form.Load?
I want to run some initialize code before the control first shown.
Also, it would be nice to be able to do the same for System.Windows.Forms.ToolStripStatusLabel which is not actually a Control, but works like one.
Ideally, I can do this:
control.OnLoad(() => { dosomething here; });
in which OnLoad is a extension method that would run the argument Action when the "control" "Loads".
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Form.Load
事件由OnLoad
方法调用,该方法是从属于Control
的OnCreateControl
方法调用的班级。因此,对于表单,调用顺序如下:我想您可以为您的组件重写
OnCreateControl
并在其中添加优化代码。希望这有帮助,问候。
Form.Load
event is called by theOnLoad
method which is called from theOnCreateControl
method which belongs to theControl
class. So for the form the calling sequence would be following:I guess you can override
OnCreateControl
for your component and add your optimization code there.Hope this helps, Regards.
对于控件,您可以重写 OnControlCreated 或 OnHandleCreated。如果需要重新创建控制窗口,后一个可以多次触发。如果您的代码影响窗口本身,请务必使用它。换句话说,如果您执行任何需要 Handle 属性的操作。
对于 ToolStripItem 派生控件来说,几乎没有合适的选择。我建议重写 SetVisibleCore() 或 OnAvailableChanged() 或 AvailableChanged 事件。它们在 ToolStripItem 的 Visible 属性更改时运行。请注意,它可能会多次触发,请保留一个布尔字段来跟踪初始化代码已经运行。
最后但并非最不重要的一点是,请确保仅当您的代码确实需要创建控件时才执行此操作。绝大多数初始化代码可以放在构造函数中。如果您的代码取决于控件的实际位置和大小,则仅需要 Load 事件。如果表单由于目标计算机上不同的系统字体或视频 DPI 设置而自行重新缩放,则这可能与设计者值不同。
For a control you can override either OnControlCreated or OnHandleCreated. The latter one can fire multiple times if it is necessary to recreate the control window. Be sure to use it if your code affects the window itself. In other words, if you do anything that requires the Handle property.
Few suitable choices for a ToolStripItem derived control. I'd recommend overriding SetVisibleCore() or OnAvailableChanged() or the AvailableChanged event. They run when the Visible property of the ToolStripItem changes. Beware that it may fire multiple times, keep a bool field that tracks that your initialization code has already run.
Last but not least, be sure to only do any of this if your code actually requires the control to be created. The vast majority of init code can go in the constructor. You only need a Load event if your code depends on the actual Location and Size of the control. Which might be different from the designer value if the form rescales itself due to a different system font or video DPI setting on the target machine.
我需要为
TabControl
中的TabPage
提供这样的解决方案。我唯一想到的就是使用绘制事件处理程序。我添加了 Paint 的事件处理程序,并在第一行中删除了事件处理程序,然后执行更多初始化代码。这仅在您没有任何自定义绘画时才有效。或者,如果您确实需要进行自定义绘画,您可以添加一个标志来检查每次 Paint 执行的时间。I needed a solution like this for a
TabPage
within aTabControl
.The only thing I came up with was using the paint event handler. I added the event handler for Paint and in the very first line I remove the event handler and then do more initialization code. This only works if you do nothave any custom painting. Alternatively, if you do need to do custom painting you could add a flag to check for each time Paint Executes.