MonthCalendar: Doppelklick

Microsoft Visual Basic .NET Express
Antworten
localhorst
Beiträge: 571
Registriert: 17.12.2012, 20:19

MonthCalendar: Doppelklick

Beitrag von localhorst »

Der MonthCalendar unterstützt standardmäßig kein Doppelklick-Event. Mit der folgenden Klasse wird ein neues Kalender-Steuerelement erzeugt, welches genau dieses Event unterstützt (das Steuerelement findet sich dann in der Toolbox wieder).

Code: Alles auswählen

Public Class ExtendedMonthCalendar
    Inherits MonthCalendar

    Private m_LastClickPosition As Point
    Private m_LastClickTime As Long
    Private m_LastClickRaisedDoubleClick As Boolean

    Public Shadows Event DoubleClick( _
        ByVal sender As Object, _
        ByVal e As EventArgs _
    )

    Protected Overrides Sub OnDoubleClick(ByVal e As EventArgs)
        RaiseEvent DoubleClick(Me, e)
    End Sub

    Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
        If e.Button = MouseButtons.Left Then
            If _
                Not m_LastClickRaisedDoubleClick AndAlso _
                Now.Ticks - m_LastClickTime <= _
                SystemInformation.DoubleClickTime * 10000 AndAlso _
                IsInDoubleClickArea(m_LastClickPosition, Cursor.Position) _
            Then
                OnDoubleClick(EventArgs.Empty)
                m_LastClickRaisedDoubleClick = True
            Else
                m_LastClickRaisedDoubleClick = False
            End If
            m_LastClickPosition = Cursor.Position
            m_LastClickTime = Now.Ticks
        End If
        MyBase.OnMouseDown(e)
    End Sub

    Private Function IsInDoubleClickArea( _
        ByVal Point1 As Point, _
        ByVal Point2 As Point _
    ) As Boolean
        Return _
            Math.Abs(Point1.X - Point2.X) <= SystemInformation.DoubleClickSize.Width AndAlso _
            Math.Abs(Point1.Y - Point2.Y) <= SystemInformation.DoubleClickSize.Height
    End Function
End Class
Anwendung in der Form:

Code: Alles auswählen

Private Sub calCalendar_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calCalendar.DoubleClick
   MsgBox("test")
End Sub
Quelle: http://dotnet.mvps.org/dotnet/faqs/?id= ... ck&lang=en
Antworten