Form_Load() 'event' or Override OnLoad()

后端 未结 3 1575
滥情空心
滥情空心 2020-11-27 06:50

I would like someone to try and explain the difference between these. More specifically, example usage scenario\'s.

I am refactoring some Windows Form

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 07:21

    When overriding OnLoad, the call to base.OnLoad invokes the Load-event of the Form.

    protected override void OnLoad(EventArgs e)
    {
      // do stuff before Load-event is raised
      base.OnLoad(e);
      // do stuff after Load-event was raised
    }
    

    If you don't specifically need to perform stuff before the Load-event is raised, placing the code in OnLoad after base.OnLoad(e) gives the same runtime behaviour as placing it in the Form_Load event handler.

    I would recommend overriding the method rather than subscribing to the event.

提交回复
热议问题