1. Herfried K. Wagner’s VB.Any
  2. .NET
  3. Häufig gestellte Fragen

Erweitern des MonthCalendar-Steuerelements um ein DoubleClick-Ereignis

Erweitern des MonthCalendar-Steuerelements um ein 'DoubleClick'-Ereignis
<URL:https://dotnet.currifex.org/dotnet/faqs/monthcalendardoubleclick/de/>
----------------------------------------------------------------------------

Extending the monthcalendar control by a 'DoubleClick' event

The Windows Forms monthcalendar control does not provide a 'DoubleClick'
event out of the box.  However, the control can be extended easily to raise
such an event.  The listing below shows the code of a class called
'ExtendedMonthCalendar' that inherits from
'System.Windows.Forms.MonthCalendar' and implements logic for detection of
double-clicks:

\\\
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
///