首页 > 解决方案 > 现有处理程序“未找到”

问题描述

我创建了一个自定义控件,继承自 DateTimePicker 来处理“连续输入”。

Public Class TimePicker
    Inherits System.Windows.Forms.DateTimePicker

    Private keyPressed As Boolean = False
    Private switchPart As Boolean = True

    Public Sub New()
        MyBase.New()
    End Sub

    Private Shadows Sub KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        switchPart = True
        If e.KeyCode >= Keys.NumPad0 AndAlso e.KeyCode <= Keys.NumPad9 Then keyPressed = True
        If e.KeyCode = Keys.Left OrElse e.KeyCode = Keys.Right Then switchPart = False
    End Sub

    Public Shadows Sub ValueChanged(sender As Object, e As System.EventArgs) Handles MyBase.ValueChanged
        ' Setup variable to store last value chosen between changes
        Static lastValue As Date = Date.FromOADate(0)
        If (Value.Hour <> lastValue.Hour OrElse Value.Minute <> lastValue.Minute) AndAlso keyPressed AndAlso switchPart Then
            SendKeys.Send("{RIGHT}")
            keyPressed = False
        End If
        lastValue = Value
    End Sub

End Class

多年来我很高兴。但最近,它开始拒绝编译使用以下代码的表单:

Private Sub dtpStartingTime_ValueChanged(sender As Object, e As EventArgs) Handles dtpStartingTime.ValueChanged
    dtpEndingTime.Value = New DateTime(Math.Max(dtpStartingTime.Value.Ticks, dtpEndingTime.Value.Ticks))
End Sub

Handles 部分中的 ValueChanged 带有红色下划线,消息为

错误 BC30590 找不到事件“ValueChanged”。

但是,如果我在带下划线的事件上单击 F12,它会将我带到自定义控件中定义的函数。所以肯定找到了。

怎么了?我已经清理了 10 次解决方案,确保删除 bin 文件夹。它不再起作用了。

有人可以帮帮我吗?

谢谢你。

PS:我用的是VS2019

标签: vb.netcustom-controls

解决方案


你认为这是什么意思:

Public Shadows Sub ValueChanged

You are specifically telling your class to hide the member with the same name in the base class. Hiding an event with a method is just plain bad.

That's not how you change behaviour on events. In well-written code, every event has a corresponding method that exists specifically to raise that event. For more information, read here. You should be overriding the appropriate methods like so:

Public Class TimePicker
    Inherits DateTimePicker

    '...

    Protected Overrides Sub OnKeyDown(e As KeyEventArgs)
        'Code here is executed before any event handlers.

        MyBase.OnKeyDown(e)

        'Code here is executed after any event handlers.
    End Sub

    Protected Overrides Sub OnValueChanged(e As EventArgs)
        'Code here is executed before any event handlers.

        MyBase.OnValueChanged(e)

        'Code here is executed after any event handlers.
    End Sub

End Class

推荐阅读