寻找控件的后构造函数事件 [英] Looking for a Post-Constructor Event of a Control

查看:208
本文介绍了寻找控件的后构造函数事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个 Label 子类,并需要初始化其几个属性。



一些我可以在构造函数中设置,但其他人正在窗体的设计器代码中重置,因此必须设置


$在设计者完成其 InitializeComponent
  • 之后但在<$ c之前的b $ b

    • $ c> Paint 事件运行,需要它们。





    我现在正在使用一个解决方法:我设置一个标志 bool needsInit = true; Paint 事件中检查的如果为true,我调用 doInit()方法,清除标志并进行初始化。



    <对于运行的窗体和VS设计器窗口..但我闻到一个标志



    所以,也许有一个更好的,做吗?

    解决方案

    覆盖InitLayout方法。

      protected override void InitLayout()
    {
    //这里做一些事情
    base.InitLayout();
    }


    I have made a Label subclass and need to initialize a few of its Properties.

    Some I can set in the constructor, but others are being reset in the designer code of the form, so they must be set

    • after the designer has done its InitializeComponent
    • but before the Paint event runs, which needs them in place.

    Of course the control should be self-sufficient, so I can't add anything to the form's code.

    I am using a workaround now: I set a flag bool needsInit = true; which I check in the Paint event. If true I call a doInit() method, which clears the flag and does the initializations.

    It works, both for the running Form and for the VS Designer window.. but I smell a flag

    So is there maybe a better, flagless way to do it? Or the Right Way?

    解决方案

    Override the InitLayout method.

    protected override void InitLayout()
    {
        // do something here
        base.InitLayout();
    }
    

    这篇关于寻找控件的后构造函数事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆