控件级别上的哪个事件等同于Form.Load事件?

时间:2018-02-19 10:15:15

标签: vb.net winforms custom-controls

我正在尝试在我的应用程序中创建多个自定义控件,例如:

Public Class CbsDataGridView
    Inherits DataGridView

    '...

    Private Sub CbsDataGridView_Enter(sender As Object, e As EventArgs) Handles MyBase.Enter
        'Code here omitted, not related to the question.
        LoadGrid()
    End Sub

    Private Sub LoadGrid()
        'Code here omitted, not related to the question.
    End Sub

    '...

End Class

Public Class CbsDateTimePicker
    Inherits DateTimePicker

    Private Sub CbsDateTimePicker_Enter(sender As Object, e As EventArgs) Handles MyBase.Enter
        'Code here omitted, not related to the question.
    End Sub

End Class

将这些控件添加到新的空表单时。以下是我遇到的两种情况:

情景1:

- Drag And Drop CbsDateTimePicker into the form
- Drag And Drop CbsDataGridView into the form
- Run Application - Load The New Form
- CbsDateTimePicker_Enter event fires.
- CbsDataGridView_Enter event doesn't fire.

情景2:

- Drag And Drop CbsDataGridView into the form
- Drag And Drop CbsDateTimePicker into the form
- Run Application - Load The New Form
- CbsDataGridView_Enter event fires.
- CbsDateTimePicker_Enter event doesn't fire.

我想我错过了控件的Enter事件。

我正在寻找的是一个类似Form.Load事件的事件,当包含控件的表单加载时会触发该事件。

是否有实现此功能的直接方法?或者我应该寻找另一种方式?

1 个答案:

答案 0 :(得分:1)

只要控件通过鼠标或键盘激活,

Enter事件就会触发。此外,每当您按代码设置表单的活动控件时,事件都将触发。

控件没有与Load Load事件类似的Form事件,但它们有一个虚拟OnCreateControl,在首次创建控件时会调用该事件。您可以覆盖它并向该方法添加自定义逻辑,甚至为您的控件引发自定义Load事件。