【发布时间】:2018-02-19 10:15:14
【问题描述】:
我正在尝试在我的应用程序中创建多个自定义控件,例如:
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.
我想我没有理解 Controls 的 Enter 事件。
我正在寻找的是一个类似于Form.Load 事件的事件,该事件将在包含控件的表单加载时触发。
有没有直接的方法来实现这个功能?还是我应该寻找其他方式?
【问题讨论】:
-
Enter 事件将在控件使用鼠标或键盘获得焦点时触发。此外,每当您通过代码设置表单的活动控件时,该事件都会触发。
-
@RezaAghaei 这解释了我得到的不良行为。无论我首先添加什么控件,都会得到关注,因此会触发
Enter事件。对吗? -
基于
TabIndex,他们在表单加载时获得焦点。
标签: vb.net winforms custom-controls