I revised the code you posted and tested it. I use VB.net 2002...ie
framework 1.0, but I think if you paste this into any vb.net form module, it
should work.
The first thing I saw that might have caused a problem was using
Timer.Enabled instead of Timer.Start.
What this code does is look for a 1 month change in date, and a click above
a certain Y coordinate. That coordinate should be a value that reflects
generally that the click occurred in the top part of the calendar. It could
vary depending on the font of the calendar because the font determines the
size of the calendar. My tests showed that a value of 35 would work for a
font size of 10, so that is what I used.
If you need to do precise measurements as to where the user clicked, the
tests for x and y can be done from the mousedown event, and shown for testing
purposes in the title bar. If you need to use precise measurements to
specifically identify that the user has clicked exactly on one arrow or the
other, remember that the coordinates will be specific to a given font setting.
[Also, I would recommend creating a form by specifically adding a windows
form to the project, rather than just adding a class that inherits the
windows form class.]
In testing, I noticed that if the user clicks on the year, or changes the
year, the mousedown event does not fire. That does not affect the specific
purpose of this code, but is worth noting for other variations.
Here's the code I tested. If you want to step through the code, you have to
put a separate breakpoint on the timer_tick event handler.
This code goes after the Windows Form designer generated code...
Private DateRangeEventArgs As DateRangeEventArgs
Private Ycoordinate As Int32 = -1
Private CurrentDate As DateTime
Private myTimer As New System.Windows.Forms.Timer()
Private Sub Form_Load(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
AddHandler myTimer.Tick, AddressOf TimerEventProcessor
myTimer.Interval = 100
Me.MonthCalendar1.SelectionStart = Date.Today
Me.MonthCalendar1.SelectionEnd = Date.Today
Me.CurrentDate = Date.Today
End Sub
Private Sub MonthCalendar1_DateChanged(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles _
MonthCalendar1.DateChanged
Me.DateRangeEventArgs = e
myTimer.Start()
End Sub
Private Sub TimerEventProcessor(ByVal myObject As Object, ByVal _
myEventArgs As EventArgs)
myTimer.Stop()
Try
'Test if Date is 1 month different and whether user clicked in
the upper area
If Me.CurrentDate.AddMonths(1) = Me.DateRangeEventArgs.Start
AndAlso _
Me.Ycoordinate > -1 AndAlso Me.Ycoordinate < 35 Then
Me.Text = "Right Arrow Clicked"
ElseIf Me.CurrentDate.AddMonths(-1) =
Me.DateRangeEventArgs.Start AndAlso _
Me.Ycoordinate > -1 AndAlso Me.Ycoordinate < 35 Then
Me.Text = "Left Arrow Clicked"
Else
Me.Text = "No Arrow Clicked"
End If
'Reset form-wide variables and objects
Me.Ycoordinate = -1
Me.CurrentDate = Me.DateRangeEventArgs.Start
Me.DateRangeEventArgs = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message & ex.StackTrace)
End Try
End Sub
Private Sub MonthCalendar1_MouseDown(ByVal sender As Object, ByVal _
e As System.Windows.Forms.MouseEventArgs) Handles _
MonthCalendar1.MouseDown
Me.Ycoordinate = e.Y
End Sub
End Class
Does this work?