Capture Month Change with MonthCalendar<[email protected]>

  • Thread starter Thread starter Annie McCall
  • Start date Start date
A

Annie McCall

Hi Randy,

I am having the same problem. I need to know whether they clicked on the arrow to change the month or actually selected a date.

I have you had any luck solving this? Can you post your solution?

In the meantime, I will continue researching for some solution. I will post if I have any success.

This seems so basic. I'm sure we'll find a solution.

Thanks,
Annie

EggHeadCafe - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 
Hi Randy,
I found a solution that works very well for me.

private void monthCalendarDate_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e) {

MonthCalendar.HitTestInfo oHTI = monthCalendarDate.HitTest(e.X,e.Y);

if ( oHTI.HitArea == MonthCalendar.HitArea.PrevMonthButton){
MessageBox.Show("Left arrow hit");
return;
}

if ( oHTI.HitArea == MonthCalendar.HitArea.NextMonthButton)
{
MessageBox.Show("Right arrow hit");

return;

}

if ( oHTI.HitArea == MonthCalendar.HitArea.Date ||
oHTI.HitArea == MonthCalendar.HitArea.PrevMonthDate ||
oHTI.HitArea == MonthCalendar.HitArea.NextMonthDate){
this.txtJobDate.Text =
this.dateValue_Renamed; this.monthCalendarDate.Visible = false;

}
}


You can detect in MouseDown event using the hitarea. In my case I only
wanted to set the visible property of my monthcalendar to false if they
actually entered a date.

dateVakye_Renamed is a global variable I set in the datechange event.

Hope this helps you.

Annie
 
Annie,
I think that your solution is just what I am looking for. However, I
don't think that you posted it in VB.NET - at least it isn't familar
to me. I worked through it in my version and here is what I came up
with:

Imports System.Windows.Forms.MonthCalendar
Public Class Form3

Private Sub MonthCalendar1_MouseDown(ByVal sender As Object, ByVal
e As System.Windows.Forms.MouseEventArgs) Handles
MonthCalendar1.MouseDown
Dim oHTI As Object
oHTI = MonthCalendar1.HitTest(e.X, e.Y)
If oHTI = MonthCalendar1.HitArea.NextMonthButton Then
Me.Text = "Right Arrow Clicked"
ElseIf oHTI = MonthCalendar1.HitArea.PrevMonthButton Then
Me.Text = "Left Arrow Clicked"
Else
Me.Text = "Date Selected"
End If
End Sub
End Class

One problem that arises is that the variable oHTI doesn't work as
object type. I am getting an error where I try to test it in the If
statement. "Access of shared member, constant member, enum member or
nested type through an instance; qualifying expression will not be
evaluated." Can you help me with resolving this?

Also, as Charlie pointed out, the MouseDown event fires after the
DateChanged event, so I still have the order of operation issue. Did
your solution address this issue?

Thank you very much. Any help is appreciated.
Randy
 
Charlie,
I've been working at implementing your solution and have a few
questions:
DateRangeEventArgs until the timer fires...Private DateRangeEventArgs
as DateRangeEventArgs."

Not sure how to do this. I know how to work with date variables, but
not with the type 'DateRangeEventArgs'. Can you show me how to
capture these?

I've created a blank form with nothing but a MonthCalendar on it to
strip this issue down to the basics.

Here is the code that I have written thus far. Do you see where it is
going wrong? Things seem to work correctly on the first pass, but
subsequent passes don't yield the correct answer.

Thanks again for your help.
Randy


Imports System.Windows.Forms

Public Class Form1

Private DateRangeEventArgs As DateRangeEventArgs
Private MouseEventArgs As MouseEventArgs
Dim bArrow As Boolean = False
Private Shared myTimer As New System.Windows.Forms.Timer()

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
AddHandler myTimer.Tick, AddressOf TimerEventProcessor
myTimer.Interval = 100
End Sub

Private Sub TimerEventProcessor(ByVal myObject As Object, ByVal
myEventArgs As EventArgs)
myTimer.Stop()
If bArrow = False Then
Me.Text = "Date Selected"
Else
Me.Text = "Arrow Clicked"
End If

Me.MouseEventArgs = Nothing
Me.DateRangeEventArgs = Nothing
End Sub

Private Sub MonthCalendar1_DateChanged(ByVal sender As Object,
ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles
MonthCalendar1.DateChanged
DateRangeEventArgs =
myTimer.Enabled = True
Application.DoEvents()
End Sub

Private Sub MonthCalendar1_MouseDown(ByVal sender As Object, ByVal
e As System.Windows.Forms.MouseEventArgs) Handles
MonthCalendar1.MouseDown
Dim x As Integer = e.X
Dim y As Integer = e.Y

If x >= 5 Then
If x <= 20 Then
If y >= 11 Then
If y <= 26 Then
'Me.Text = "Left Arrow Clicked"
bArrow = True
End If
End If
End If
End If

If x >= 206 Then
If x <= 221 Then
If y >= 11 Then
If y <= 26 Then
'Me.Text = "Right Arrow Clicked"
bArrow = True
End If
End If
End If
End If

End Sub


End Class
 
One problem that arises is that the variable oHTI doesn't work as
object type. I am getting an error where I try to test it in the If
statement. "Access of shared member, constant member, enum member or
nested type through an instance; qualifying expression will not be
evaluated." Can you help me with resolving this?

Scratch that. I figured out how to define and work with the
variable. My only issue is with the order of the DateChanged and
MouseDown events. I'm working with Charlie's timer solution, but
can't quite master it just yet (see above). Thanks to anybody who can
help.

Randy
 
Back
Top