Find Mouse Coordinates Within Control

R

Randy

I'm struggling with identifying whether a user has clicked on the left
or right arrows within the MonthCalendar controls. I had a good
suggestion to trap the mouse coordinates that the user clicks to
determine if they have clicked within the region where the arrow
exists. My problem is that my form is not maximized and therefore the
control can exist almost anywhere on the user's screen. I thought
about forcing the form to a fixed position, but I'd rather not do this
unless I have to.

What I thought might work would be to calculate the coordinate that
the user clicked relative to the position of the form since the
control is always in the same place relative to the form itself. Here
is the code that I am working on (excerpted):

Private Sub MonthCalendar1_DateChanged(ByVal sender As Object, ByVal e
As System.Windows.Forms.DateRangeEventArgs) Handles
MonthCalendar1.DateChanged
Dim x As Integer = Val(System.Windows.Forms.Cursor.Position.X) -
Val(Me.MonthCalendar1.Location.X)
Dim y As Integer = Val(System.Windows.Forms.Cursor.Position.Y) -
Val(Me.MonthCalendar1.Location.Y)
Dim d As Date = e.Start
If x < ????? Then
If y < ????? Then
Me.Text = x & ", " & y & " Left arrow clicked"
End If
End If
End Sub


I thought that this would work, but I'm not getting predictable values
of x and y. Can anybody help me figure out how to do this?

Thanks,
Randy
 
G

Guest

If you use the mousedown event, the x and y coordinates available in the
mouseeventargs are relative to the control. Because the mousedown event
happens after the datechanged event, you have to do a workaround to
effectively reverse the order.

I know this may seem a crude way to go about it, but if you create a timer
(form level, withevents). Set the timer's duration for 100ms in the
Form_Load event.
Create an event handler for Timer_Tick.

Create a form level variable that will hold your DateRangeEventArgs until
the timer fires...Private DateRangeEventArgs as DateRangeEventArgs. That way
the timer's tick event handler can act as a delayed proxy for your
DateChanged event handler. Just port the code over.

After you stash the DateRangeEventArgs, Start the timer in the date changed
event handler. During the 100ms while the Timer is waiting to fire, the
mousedown event will fire and there you can stash the MouseEventArgs in a
form level variable. The MouseEventArgs contains your mouse X and Y
coordinates relative to the control.

In the timer_tick event handler, stop the timer, and then do your tests:
the two addmonth tests, as mentioned in previous message, and if either of
those are true, test if Me.MouseEventArgs.Y is less than 24 (or whatever your
click testing indicates...actually you could fudge it to allow a few extra
hundredths and be safe). If the user uses the keyboard arrow keys, instead
of the mouse, to change dates, there will be no mousedown event, and
therefore no MouseEventArgs...handle that as needed.

Lastly, in the timer routine, reset your applicable formwide variables to
their wait-state values...
Me.MouseEventArgs = Nothing, Me.DateRangeEventArgs = Nothing

You may want to adjust the timer's interval, but I think 100ms should be
good. You may be able to take it down to 50 or so. I have tried using a new
Thread for this kind of workaround, but I find the Timer to be more
predictable and reliable.
 
A

Annie McCall

I answered same on your other thread on MonthCalendar.


View Message Thread (11 replies)

« Previous Page Results 11 - 12 of 12

Capture Month Change with MonthCalendar
From: Unknown User
Date Posted: 10/14/2007 8:42:00 AM



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



Re: Capture Month Change with MonthCalendar
From: Annie McCall
Date Posted: 10/14/2007 10:26:00 AM



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.

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

Hope this helps you.




Annie McCall
 
A

Annie McCall

I answered same on your other thread on MonthCalendar.


View Message Thread (11 replies)

« Previous Page Results 11 - 12 of 12

Capture Month Change with MonthCalendar
From: Unknown User
Date Posted: 10/14/2007 8:42:00 AM



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



Re: Capture Month Change with MonthCalendar
From: Annie McCall
Date Posted: 10/14/2007 10:26:00 AM



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.

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

Hope this helps you.




Annie McCall
 
G

Guest

Annie,

I replied to one of Randy's 10/14 messages (in the thread that he started on
10/6) with some code I wrote and tested.
 

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