How to select a region on a form

  • Thread starter Thread starter Dino M. Buljubasic
  • Start date Start date
D

Dino M. Buljubasic

I am trying to build a daily calendar similar to Microsoft Outlook where a
user can select hours of a day using mouse. I have a form that holds a
panel which contains user controls each representing an hour of the day (24
hours = 24 user controls).

How can I mouse-down, drag, mouse-up select a region (say from 9:00am to
1:00pm)?

I was trying to use mouse down/mouse up events and return the tag property
of the user controls on down / up but even though I drag with the mouse over
several controls, both of these events return tag property of the user
control that received mouse down event.

Any help will be greatfully appreciated
Dino
 
I am trying to build a daily calendar similar to Microsoft Outlook where a
user can select hours of a day using mouse. I have a form that holds a
panel which contains user controls each representing an hour of the day (24
hours = 24 user controls).

How can I mouse-down, drag, mouse-up select a region (say from 9:00am to
1:00pm)?

I was trying to use mouse down/mouse up events and return the tag property
of the user controls on down / up but even though I drag with the mouse over
several controls, both of these events return tag property of the user
control that received mouse down event.

Any help will be greatfully appreciated
Dino

I had a similar problem. This is how I solved it:
1) Catch MouseDown, MouseMove and MouseUp in the user control.
2) Implement a static function that finds the user control located at
a particular point.

e.g.
Private Shared GetControlAt(mousePoint As Point) _
As MyUserContol

3) Implement a class that contains the rectangle being selected (top
-left and bottom-right points - initial mouse down and current mouse
position).

This enables you to redirect MouseMove events (which will be initially
received by the same control you did the mouse down on) to the
appropriate control, if needs be. Therein, you can test to see if it
is in the selected region.
So, in each of the mouse events, the first check should be to see that
the mouse pointer is in the current control. If it is not, redirect
the event to the proper control. If it is, do your selection test
and/or selection size adjustment.

i.e.
...MouseMove...
MyUserControl ctrl = GetControlAt( mouseLocation )
If ctrl Is Me Then
' Do the selection, or whatever
ElseIf Not ctrl Is Nothing Then
' Redirect this to the proper control
ctrl_MouseMove...
End If


hth

Andy
 
Hi Andy,

thanks for reading this post and for your reply. The only thing I don't
understand is how to redirect mouse move event to another control. Can you
explain me that?

Thank you,
Dino
 
Hi Andy,

thanks for reading this post and for your reply. The only thing I don't
understand is how to redirect mouse move event to another control. Can you
explain me that?

Thank you,
Dino

By "redirection" I simply meant the act of calling the handler of
another instance of the class (instead of this one itself). It's taken
care of by the "If ctrl Is Me Then" line. The logic is this:

If the mouse pointer is within the rectangle of this control
Handle the event
Else
Find the control that is under the pointer
Call the handler of that control instead
End If

What I did fail to mention is the use of "Form.GetChildAtPoint()" to
determine which control the mouse is over.

hth
 
Hi Andy,

I am having difficulties to get the point at Mouse_Down, Mouse_Up. My form
is completelly covered with dynamically loaded user controls that are docked
so that all form area is covered.
When I click on say hour 11:00am (which is a user control), I get point
relevant to that user control, not to form. Therefore if I click say close
to the top of any of my controls, I get always the same or approximatelly
the same point (say 10, 10). This means I clicked mouse button down at
point 10, 10 inside that control. Now when I use that point to get the
control on it, I get some other control that is close to the top of the FORM
(since my Mouse_Down/Mouse_Up events are of the form), and the control on
the form at that point is in my case a toolbar, not a user control
representing an hour of a day. But I need to get the coordinates relevant
to the form so I can get the control I need.

Can you help me with this please?

Regards, Dino
 
Hi Andy,

sorry for annoying you :) but I have a problem that I can not solve and was
hoping you could help me with that.

I am trying to get coordinates of mouse_down/mouse_up event BUT relevant to
the form or the parent of a control, not to the control where mouse click
has occured. That means, even if I click a control on the form, I want to
get the coordinates of the form of the click event, not coordinates of the
control that was clicked on.

My user controls are on a panel pnToday. Each user control has two text
boxes inside representing whole and half hour (e.g. 11:00 am and 11:30am)
Currently I am doint it as:

' add event handler to all dynamically loaded user controls
AddHandler ctype(MyUserControl.TextBox1, TextBox).MouseDown, AddressOf
pnToday_MouseDown
AddHandler ctype(MyUserControl.TextBox2, TextBox).MouseDown, AddressOf
pnToday_MouseDown
AddHandler ctype(MyUserControl.TextBox1, TextBox).MouseUp, AddressOf
pnToday_MouseUp
AddHandler ctype(MyUserControl.TextBox2, TextBox).MouseUp, AddressOf
pnToday_MouseUp

' then in my mouse down / up events for the panel (parent of user controls)
Private Sub pnToday_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles pnToday.MouseDown
ptEntryPoint = New Point(e.X, e.Y)
EntryTextBox = Me.GetChildAtPoint(ptEntryPoint)
'MsgBox(EntryTextBox.GetType.ToString) ' this should retrun type of
control clicked but it returns type of my toolbar
End Sub

Private Sub pnToday_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles pnToday.MouseUp
ptExitPoint = New Point(e.X, e.Y)

ExitTextBox = Me.GetChildAtPoint(ptExitPoint)

MsgBox("Entry at (" & ptEntryPoint.X & " ; " & ptEntryPoint.Y & ")"
& vbCrLf & _
"Exit at (" & ptExitPoint.X & " ; " & ptExitPoint.Y & ")")

MsgBox("Entry Hour: " & CType(EntryTextBox, TextBox).Tag & " " &
" Exit Hour: " & CType(ExitTextBox, TextBox).Tag) ' this crashes because of
invalid type conversion from toolbar to textbox, because the point returned
is not relevant to parent container of user controls.
End Sub

Regards,
Dino
 
Back
Top