473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Capture Month Change with MonthCalendar

I have a MonthCalendar on one of my forms. I have disovered that the
DateChanged event is triggered not only when the user clicks on a new
date, but also if they click on the Previous or Next Month arrows
(meaning they click on either of the left or right pointing arrows in
the top corner). If the user changes the month, it messes up some of
the date logic that I have in the DateChanged event. Does anybody
know how I can capture this action? I'd like my code in the
DateChanged routine to work like this:

If MonthNotChanged then
Do Something
Else 'month changed
Do something Else
End if

Thanks,
Randy

Oct 7 '07 #1
11 7340
Randy,

Did you try the other events, I use always the TextChanged one?

Cor
Oct 7 '07 #2
I don't see TextChanged in the list of events. I looked at the other
events, but I don't see anything that is more appropriate.

Oct 7 '07 #3

http://msdn2.microsoft.com/en-us/lib...xtchanged.aspx
>I don't see TextChanged in the list of events. I looked at the other
events, but I don't see anything that is more appropriate.
Oct 8 '07 #4
Charlie,
I was going down the same path with using an If statement to check if
the month had changed. Unfortunately, this doesn't always work. If
you look at the calendar view, you can often see the last few days of
the previous month or the first few days of the succeeding month. If
the user were to click on these dates, the logic you propose would
conclude that the user had clicked the arrow, not the date. I'll post
my code below for anybody who is interested.

What I am trying to do is execute certain lines of code *only* if the
user has selected a date. If they are clicking on one of the arrows,
I am treating this action as the user *navigating* to a date (as
opposed to actually selecting one). So if they were to click Sep 28
while viewing the Oct calendar, this would qualify as *selecting* a
date, and thus would initiate a sequence of actions. If they clicked
the left arrow, this is simply a navigational move and would be
disregarded. I've looked for patterns in how the MonthCalendar
handles these arrow clicks, but I'm not seeing reliable patterns that
I can code around.

Somehow, the control must distinguish these arrows or it wouldn't work
as it does. So the question is still, how can I trap these clicks in
my VB code? If I can figure that out, it will make my programming
monumentally easier as well as more reliable.
Private Sub MonthCalendar1_DateChanged(ByVal sender As Object, ByVal e
As System.Windows.Forms.DateRangeEventArgs) Handles
MonthCalendar1.DateChanged
'If there are unsaved changes, alert the user
If bLoading = False Then
If bUnsaved = True Then
frmDateChange.Show()
Exit Sub
End If

'This is where I'm stuck trying to trap the arrow click
Dim d As Date = e.Start
'Me.Text = Me.CurrentSelectedDate
If d.AddMonths(-1).Month = Me.CurrentSelectedDate.Month
Then
If d.Day = 1 Then
Me.Text = d.ToString & " Right arrow clicked."
Call SelectDates()
End If
ElseIf d.AddMonths(1).Month = Me.CurrentSelectedDate.Month
Then
If d.Day = 1 Then
Me.Text = d.ToString & " Left arrow clicked."
Call SelectDates()
End If
Else
Me.Text = d.ToString & " Date changed."
Call SelectDates()
Call RemoveControls()
Call AddDataRows()
End If
Me.CurrentSelectedDate = d
End If
End Sub

Private Sub SelectDates()
Dim i As Integer =
CInt(MonthCalendar1.SelectionStart.DayOfWeek)
Dim d As Date = MonthCalendar1.SelectionStart
Dim a As Integer
If i = 0 Then
a = -6
Else
a = 1 - i
End If
MonthCalendar1.SelectionStart = d.AddDays(a)

If i = 0 Then
a = 0
Else
a = 7 - i
End If
MonthCalendar1.SelectionEnd = d.AddDays(a)
End Sub

So, how do I trap this arrow click? Or if there is no way, how can I
program the logic to identify and trap the conditions that would would
be present if the arrows are clicked? Any help is appreciated.

Thanks,
Randy

Oct 9 '07 #5
You could add another event handler to the code I previously provided...In
fact, this would probably do it on its own...

The MonthCalendar.MouseDown event (e as MouseEventArgs) captures the X and Y
coordinates relative to the control itself...My test shows that Y < 24 for a
click of the month buttons...

"Randy" wrote:
Charlie,
I was going down the same path with using an If statement to check if
the month had changed. Unfortunately, this doesn't always work. If
you look at the calendar view, you can often see the last few days of
the previous month or the first few days of the succeeding month. If
the user were to click on these dates, the logic you propose would
conclude that the user had clicked the arrow, not the date. I'll post
my code below for anybody who is interested.

What I am trying to do is execute certain lines of code *only* if the
user has selected a date. If they are clicking on one of the arrows,
I am treating this action as the user *navigating* to a date (as
opposed to actually selecting one). So if they were to click Sep 28
while viewing the Oct calendar, this would qualify as *selecting* a
date, and thus would initiate a sequence of actions. If they clicked
the left arrow, this is simply a navigational move and would be
disregarded. I've looked for patterns in how the MonthCalendar
handles these arrow clicks, but I'm not seeing reliable patterns that
I can code around.

Somehow, the control must distinguish these arrows or it wouldn't work
as it does. So the question is still, how can I trap these clicks in
my VB code? If I can figure that out, it will make my programming
monumentally easier as well as more reliable.
Private Sub MonthCalendar1_DateChanged(ByVal sender As Object, ByVal e
As System.Windows.Forms.DateRangeEventArgs) Handles
MonthCalendar1.DateChanged
'If there are unsaved changes, alert the user
If bLoading = False Then
If bUnsaved = True Then
frmDateChange.Show()
Exit Sub
End If

'This is where I'm stuck trying to trap the arrow click
Dim d As Date = e.Start
'Me.Text = Me.CurrentSelectedDate
If d.AddMonths(-1).Month = Me.CurrentSelectedDate.Month
Then
If d.Day = 1 Then
Me.Text = d.ToString & " Right arrow clicked."
Call SelectDates()
End If
ElseIf d.AddMonths(1).Month = Me.CurrentSelectedDate.Month
Then
If d.Day = 1 Then
Me.Text = d.ToString & " Left arrow clicked."
Call SelectDates()
End If
Else
Me.Text = d.ToString & " Date changed."
Call SelectDates()
Call RemoveControls()
Call AddDataRows()
End If
Me.CurrentSelectedDate = d
End If
End Sub

Private Sub SelectDates()
Dim i As Integer =
CInt(MonthCalendar1.SelectionStart.DayOfWeek)
Dim d As Date = MonthCalendar1.SelectionStart
Dim a As Integer
If i = 0 Then
a = -6
Else
a = 1 - i
End If
MonthCalendar1.SelectionStart = d.AddDays(a)

If i = 0 Then
a = 0
Else
a = 7 - i
End If
MonthCalendar1.SelectionEnd = d.AddDays(a)
End Sub

So, how do I trap this arrow click? Or if there is no way, how can I
program the logic to identify and trap the conditions that would would
be present if the arrows are clicked? Any help is appreciated.

Thanks,
Randy

Oct 9 '07 #6
The mousedown event happens AFTER the datechanged event, so you may have to
capture any needed parameters in the datechanged event, and let the mousedown
event act on those parameters after determining where the user clicked...

As I was writing the previous note, I assumed the events processed in
reverse to how they actually process...Oh well, it really should not be much
of a problem.

"Randy" wrote:
Charlie,
I was going down the same path with using an If statement to check if
the month had changed. Unfortunately, this doesn't always work. If
you look at the calendar view, you can often see the last few days of
the previous month or the first few days of the succeeding month. If
the user were to click on these dates, the logic you propose would
conclude that the user had clicked the arrow, not the date. I'll post
my code below for anybody who is interested.

What I am trying to do is execute certain lines of code *only* if the
user has selected a date. If they are clicking on one of the arrows,
I am treating this action as the user *navigating* to a date (as
opposed to actually selecting one). So if they were to click Sep 28
while viewing the Oct calendar, this would qualify as *selecting* a
date, and thus would initiate a sequence of actions. If they clicked
the left arrow, this is simply a navigational move and would be
disregarded. I've looked for patterns in how the MonthCalendar
handles these arrow clicks, but I'm not seeing reliable patterns that
I can code around.

Somehow, the control must distinguish these arrows or it wouldn't work
as it does. So the question is still, how can I trap these clicks in
my VB code? If I can figure that out, it will make my programming
monumentally easier as well as more reliable.
Private Sub MonthCalendar1_DateChanged(ByVal sender As Object, ByVal e
As System.Windows.Forms.DateRangeEventArgs) Handles
MonthCalendar1.DateChanged
'If there are unsaved changes, alert the user
If bLoading = False Then
If bUnsaved = True Then
frmDateChange.Show()
Exit Sub
End If

'This is where I'm stuck trying to trap the arrow click
Dim d As Date = e.Start
'Me.Text = Me.CurrentSelectedDate
If d.AddMonths(-1).Month = Me.CurrentSelectedDate.Month
Then
If d.Day = 1 Then
Me.Text = d.ToString & " Right arrow clicked."
Call SelectDates()
End If
ElseIf d.AddMonths(1).Month = Me.CurrentSelectedDate.Month
Then
If d.Day = 1 Then
Me.Text = d.ToString & " Left arrow clicked."
Call SelectDates()
End If
Else
Me.Text = d.ToString & " Date changed."
Call SelectDates()
Call RemoveControls()
Call AddDataRows()
End If
Me.CurrentSelectedDate = d
End If
End Sub

Private Sub SelectDates()
Dim i As Integer =
CInt(MonthCalendar1.SelectionStart.DayOfWeek)
Dim d As Date = MonthCalendar1.SelectionStart
Dim a As Integer
If i = 0 Then
a = -6
Else
a = 1 - i
End If
MonthCalendar1.SelectionStart = d.AddDays(a)

If i = 0 Then
a = 0
Else
a = 7 - i
End If
MonthCalendar1.SelectionEnd = d.AddDays(a)
End Sub

So, how do I trap this arrow click? Or if there is no way, how can I
program the logic to identify and trap the conditions that would would
be present if the arrows are clicked? Any help is appreciated.

Thanks,
Randy

Oct 9 '07 #7
And just in case you're not always depending on a mouse click for a date
change, you could use a timer to postpone the code of the datechanged event
(maybe 100 ms?, to see if the mouseclick occurred.

"Randy" wrote:
Charlie,
I was going down the same path with using an If statement to check if
the month had changed. Unfortunately, this doesn't always work. If
you look at the calendar view, you can often see the last few days of
the previous month or the first few days of the succeeding month. If
the user were to click on these dates, the logic you propose would
conclude that the user had clicked the arrow, not the date. I'll post
my code below for anybody who is interested.

What I am trying to do is execute certain lines of code *only* if the
user has selected a date. If they are clicking on one of the arrows,
I am treating this action as the user *navigating* to a date (as
opposed to actually selecting one). So if they were to click Sep 28
while viewing the Oct calendar, this would qualify as *selecting* a
date, and thus would initiate a sequence of actions. If they clicked
the left arrow, this is simply a navigational move and would be
disregarded. I've looked for patterns in how the MonthCalendar
handles these arrow clicks, but I'm not seeing reliable patterns that
I can code around.

Somehow, the control must distinguish these arrows or it wouldn't work
as it does. So the question is still, how can I trap these clicks in
my VB code? If I can figure that out, it will make my programming
monumentally easier as well as more reliable.
Private Sub MonthCalendar1_DateChanged(ByVal sender As Object, ByVal e
As System.Windows.Forms.DateRangeEventArgs) Handles
MonthCalendar1.DateChanged
'If there are unsaved changes, alert the user
If bLoading = False Then
If bUnsaved = True Then
frmDateChange.Show()
Exit Sub
End If

'This is where I'm stuck trying to trap the arrow click
Dim d As Date = e.Start
'Me.Text = Me.CurrentSelectedDate
If d.AddMonths(-1).Month = Me.CurrentSelectedDate.Month
Then
If d.Day = 1 Then
Me.Text = d.ToString & " Right arrow clicked."
Call SelectDates()
End If
ElseIf d.AddMonths(1).Month = Me.CurrentSelectedDate.Month
Then
If d.Day = 1 Then
Me.Text = d.ToString & " Left arrow clicked."
Call SelectDates()
End If
Else
Me.Text = d.ToString & " Date changed."
Call SelectDates()
Call RemoveControls()
Call AddDataRows()
End If
Me.CurrentSelectedDate = d
End If
End Sub

Private Sub SelectDates()
Dim i As Integer =
CInt(MonthCalendar1.SelectionStart.DayOfWeek)
Dim d As Date = MonthCalendar1.SelectionStart
Dim a As Integer
If i = 0 Then
a = -6
Else
a = 1 - i
End If
MonthCalendar1.SelectionStart = d.AddDays(a)

If i = 0 Then
a = 0
Else
a = 7 - i
End If
MonthCalendar1.SelectionEnd = d.AddDays(a)
End Sub

So, how do I trap this arrow click? Or if there is no way, how can I
program the logic to identify and trap the conditions that would would
be present if the arrows are clicked? Any help is appreciated.

Thanks,
Randy

Oct 9 '07 #8
Thanks a lot, Charlie. These are great suggestions. I'm having a
little trouble implementing it, however, as I cannot get predictable
values for the coordinates where the arrows would have to exist.
Since this is a separate topic from just determining if the arrow has
been clicked, I started a new post. (search: Find Mouse Coordinates
Within Control). I'd appreciate if you have any ideas.

Thanks again for all of your help. I really appreciate it.
Randy

Oct 11 '07 #9
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
Oct 14 '07 #10

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

*** Sent via Developersdex http://www.developersdex.com ***
Oct 14 '07 #11
Charlie,
I've been working at implementing your solution and have a few
questions:
>"Create a form level variable that will hold your
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

Oct 14 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Laguna | last post by:
Hi Gurus, I want to find the expiration date of stock options (3rd Friday of the month) for an any give month and year. I have tried a few tricks with the functions provided by the built-in...
0
by: David | last post by:
Hi, I have a MainForm with a Subform. On the Mainform are 3 Textboxes: Order Qty, Qty Shipped & Qty Outstanding Order Qty is a set figure for display purposes only Qty Shipped is a textbox...
2
by: MAX SMITH | last post by:
I'm a newbie to Access and am having trouble understanding the logic behind queries. I have 2 tables, 1 for stock in and 1 for stock out. I have joined these in a union query. the fields I have...
10
by: Ty Smith via AccessMonster.com | last post by:
I noticed that the week numbers in Stephan Leban's MonthCalendar are not consistent with Microsoft Outlook (they are shifted one week forward). Is there any way I can sync these two up by changing...
6
by: Carlos García-Carazo | last post by:
Hello, I am working on a C# application for an industrial machine, using Windows Forms, where the user could look at the screen from a 90 degree rotated position, like he could turn the monitor...
3
by: RG | last post by:
For a Windows project in VB.NET 2003: I need a Calendar that’s much larger than the MonthCalendar control in the toolbox; it needs to fill the whole screen (at least approximately ). VB...
1
by: DS | last post by:
I seem to have run into another strange anomaly with the monthCalendar control. I want to display 6 months at time with either the first half of the year (January-June) or the later half of the...
3
by: gv | last post by:
Hi all, Using VB Express 2005 How do I get the day of the first day of the month using the MonthCalendar control. so if the MonthCalendar control is set to the systems date. thanks
1
by: Gidi | last post by:
Hi, I'm using 3 months calendars control. I set one of them to show the current month (october), and I want the second and third to show one and two month forward. How can it be done?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.