Change Order of Events

R

Randy

Hi,
I have an odd issue with the MonthCalendar control. I need to capture
certain mouseeventargs, which I can only do in the MouseDown event. I
need to pass these arguments to the DateChanged event to make the
logic in that routine work correctly. Simple enough, except that the
DateChanged event fires before the MouseDown event, making it too late
to gather these arguments.

Can anybody help me figure out a way to effectively reverse the order
in which these events fire?

Thanks,
Randy

p.s. apologies if anybody has seen this as part of another posting of
mine, but that posting initially dealt with another question and has
become quite long and meandering. I am posting the question here to
isolate it so that it is clear what the issue is. Upon resolution of
this issue, I will post back to the original post with this solution
in the context of that posting.
 
R

rowe_newsgroups

Hi,
I have an odd issue with the MonthCalendar control. I need to capture
certain mouseeventargs, which I can only do in the MouseDown event. I
need to pass these arguments to the DateChanged event to make the
logic in that routine work correctly. Simple enough, except that the
DateChanged event fires before the MouseDown event, making it too late
to gather these arguments.

Can anybody help me figure out a way to effectively reverse the order
in which these events fire?

Thanks,
Randy

p.s. apologies if anybody has seen this as part of another posting of
mine, but that posting initially dealt with another question and has
become quite long and meandering. I am posting the question here to
isolate it so that it is clear what the issue is. Upon resolution of
this issue, I will post back to the original post with this solution
in the context of that posting.

Just a thought, not sure what problems this may cause:

////////////////
Public Class ExtendedMonthCalendar
Inherits MonthCalendar

Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(e)
preventDateChanged = False
OnDateChanged(dateChangedEventArgs)
End Sub

Protected preventDateChanged As Boolean = True
Protected dateChangedEventArgs As DateRangeEventArgs

Protected Overrides Sub OnDateChanged(ByVal drevent As
System.Windows.Forms.DateRangeEventArgs)
Try
If preventDateChanged Then
dateChangedEventArgs = drevent
Else
MyBase.OnDateChanged(drevent)
End If
Finally
preventDateChanged = True
End Try
End Sub

End Class
////////////////

Thanks,

Seth Rowe
 
R

Randy

Thanks, Seth. I pasted your code into my form and ran it. It
compiles without error, but it never fires. Can you take a look at
this and maybe point out where I am going wrong?

Imports System.Windows.Forms
Public Class Form3

Private Sub MonthCalendar1_DateChanged(ByVal sender As Object,
ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles
MonthCalendar1.DateChanged
Me.Text = "Order Reversed. Problem solved."
End Sub


Private Sub MonthCalendar1_MouseDown(ByVal sender As Object, ByVal
e As System.Windows.Forms.MouseEventArgs) Handles
MonthCalendar1.MouseDown
Me.Text = "Order NOT Reversed. Problem still exists."
End Sub
End Class

Public Class ExtendedMonthCalendar
Inherits MonthCalendar

Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(e)
preventDateChanged = False
OnDateChanged(dateChangedEventArgs)
End Sub


Protected preventDateChanged As Boolean = True
Protected dateChangedEventArgs As DateRangeEventArgs


Protected Overrides Sub OnDateChanged(ByVal drevent As
System.Windows.Forms.DateRangeEventArgs)
Try
If preventDateChanged Then
dateChangedEventArgs = drevent
Else
MyBase.OnDateChanged(drevent)
End If
Finally
preventDateChanged = True
End Try
End Sub

End Class

Thanks again,
Randy
 
R

rowe_newsgroups

Thanks, Seth. I pasted your code into my form and ran it. It
compiles without error, but it never fires. Can you take a look at
this and maybe point out where I am going wrong?

Imports System.Windows.Forms
Public Class Form3

Private Sub MonthCalendar1_DateChanged(ByVal sender As Object,
ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles
MonthCalendar1.DateChanged
Me.Text = "Order Reversed. Problem solved."
End Sub

Private Sub MonthCalendar1_MouseDown(ByVal sender As Object, ByVal
e As System.Windows.Forms.MouseEventArgs) Handles
MonthCalendar1.MouseDown
Me.Text = "Order NOT Reversed. Problem still exists."
End Sub
End Class

Public Class ExtendedMonthCalendar
Inherits MonthCalendar

Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(e)
preventDateChanged = False
OnDateChanged(dateChangedEventArgs)
End Sub

Protected preventDateChanged As Boolean = True
Protected dateChangedEventArgs As DateRangeEventArgs

Protected Overrides Sub OnDateChanged(ByVal drevent As
System.Windows.Forms.DateRangeEventArgs)
Try
If preventDateChanged Then
dateChangedEventArgs = drevent
Else
MyBase.OnDateChanged(drevent)
End If
Finally
preventDateChanged = True
End Try
End Sub

End Class

Thanks again,
Randy

The code I gave you is a replacement for the MonthCalendar.

If I were you, I would add a new class file to my project (call it
ExtendedMonthCalendar.vb or something similar) and then place the code
I gave you there. Then, when you rebuild your project you should have
an ExtendedMonthCalendar item listed under <Project> Components in the
Toolbox. Then you can drag that component onto a form and use it as
normal.

If you want to replace your existing MonthCalendar, you pretty much
have two options. The first is to delete the existing control and
replace it with the new ExtendedMonthCalendar. This will be fairly
annoying if you have a lot of Event Handlers wired up to the calendar.
The second option is to open up the Designer.vb file for the "host"
form (or whatever) and modify the declaration for MonthCalendar1 to
use the ExtendedMonthCalendar class instead. By the way, to do this
you'll need to first click the "Show All Files" button on the solution
explorer. Then replace lines like "Private MonthCalendar1 As
MonthCalendar" and "MonthCalendar1 = New MonthCalendar()" with lines
like "Private MonthCalendar1 As ExtendedMonthCalendar" and
"MonthCalendar1 = New ExtendedMonthCalendar()"

Let me know how it works!

Thanks,

Seth Rowe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top