为 Winforms Control 添加 Load 事件,就像 Form 类一样

发布于 2024-08-14 15:39:49 字数 345 浏览 9 评论 0原文

有没有办法像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人练习一个人 2024-08-21 15:39:49

Form.Load 事件由 OnLoad 方法调用,该方法是从属于 ControlOnCreateControl 方法调用的班级。因此,对于表单,调用顺序如下:

OnCreateControl start
  OnLoad start
      Form Load event call
  OnLoad finish
OnCreateControl finish

我想您可以为您的组件重写 OnCreateControl 并在其中添加优化代码。

希望这有帮助,问候。

Form.Load event is called by the OnLoad method which is called from the OnCreateControl method which belongs to the Control class. So for the form the calling sequence would be following:

OnCreateControl start
  OnLoad start
      Form Load event call
  OnLoad finish
OnCreateControl finish

I guess you can override OnCreateControl for your component and add your optimization code there.

Hope this helps, Regards.

那些过往 2024-08-21 15:39:49

对于控件,您可以重写 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.

勿忘初心 2024-08-21 15:39:49

我需要为 TabControl 中的 TabPage 提供这样的解决方案。我唯一想到的就是使用绘制事件处理程序。我添加了 Paint 的事件处理程序,并在第一行中删除了事件处理程序,然后执行更多初始化代码。这仅在您没有任何自定义绘画时才有效。或者,如果您确实需要进行自定义绘画,您可以添加一个标志来检查每次 Paint 执行的时间。

//Paint only runs once    
private void tabPage1_Paint(object sender, PaintEventArgs e)
{
    tabPage1.Paint -= tabPage1_Paint;    
    //Do initialization here    
}

/////////////////////////////////////////////////////////////////////////////////

//Paint always runs

private bool IsFirstPaint = true;

private void tabPage1_Paint(object sender, PaintEventArgs e)    
{    
    if(IsFirstPaint)
    {
        IsFirstPaint = false;    
        //Do initialization here
    }    
    //Do custom painting here
}

I needed a solution like this for a TabPage within a TabControl.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.

//Paint only runs once    
private void tabPage1_Paint(object sender, PaintEventArgs e)
{
    tabPage1.Paint -= tabPage1_Paint;    
    //Do initialization here    
}

/////////////////////////////////////////////////////////////////////////////////

//Paint always runs

private bool IsFirstPaint = true;

private void tabPage1_Paint(object sender, PaintEventArgs e)    
{    
    if(IsFirstPaint)
    {
        IsFirstPaint = false;    
        //Do initialization here
    }    
    //Do custom painting here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文