Multiselecting user controls

Hey,

Im working on a project which requires my application to have a calender to which i can load
the app's information.
Since its quite a customized calendar ive created 3 custom controls: the calendar which loads month controls which in turn load the day controls.

Obviously the day control is the most sophisticated one since the information is displayed on it.
This works great and also looks quite the way i wanted.

The only functionality missing is to be able to select more than one day control at a time with the mouse, kinda how you can select more than one day in the outlook calendar, just by clicking on one day and draggin the mouse to the last day needed.

Ive never done something like this with controls and i was wondering if its possible and how can i do it if it is?

Im working with VB.NET 2.0.

Thank you very much
unholly_pluginAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
It is possible...albeit a pain in the butt...

I assume the "calendar" control holds instances of "month" and "day"?

Each day instance is a self contained unit and has no knowledge of the other days so this would have to happen at the calendar level.

What you could do is declare a custom even that the day control raises when the mouse is dragged in it.  Then the calendar control can trap that event and determine what other day controls need to be highlighted as a result of the drag.

Here is the basic concept.  Try it out with a blank form.  Remember, this is just to illustrate the idea so I have kept the logic at a minimum...

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Size = New Size(450, 300)
        Dim cal As New CalendarControl
        cal.Dock = DockStyle.Fill
        Me.Controls.Add(cal)
    End Sub

    Public Class CalendarControl
        Inherits UserControl

        Private flp As FlowLayoutPanel

        Public Sub New()
            Dim dc As DayControl
            flp = New FlowLayoutPanel()
            flp.Dock = DockStyle.Fill
            flp.FlowDirection = FlowDirection.LeftToRight
            flp.WrapContents = True
            For i As Integer = 1 To 30
                dc = New DayControl(i)
                AddHandler dc.Dragging, AddressOf dc_Dragging
                flp.Controls.Add(dc)
            Next
            Me.Controls.Add(flp)
        End Sub

        Private Sub dc_Dragging(ByVal sender As DayControl, ByVal e As System.Windows.Forms.MouseEventArgs)
            Dim scrnPoint As Point = sender.PointToScreen(e.Location)
            Dim calPoint = flp.PointToClient(scrnPoint)
            Dim ctl As Control = flp.GetChildAtPoint(calPoint)
            If Not (ctl Is Nothing) Then
                If TypeOf ctl Is DayControl Then
                    Dim dc As DayControl = CType(ctl, DayControl)
                    If Not dc.selected Then
                        dc.selected = True
                        dc.Refresh()
                    End If
                End If
            End If
        End Sub

    End Class

    Public Class DayControl
        Inherits UserControl

        Private dayOfMonth As Integer
        Public selected As Boolean = False

        Public ReadOnly Property Day() As String
            Get
                Return dayOfMonth
            End Get
        End Property

        Public Event Dragging(ByVal sender As DayControl, ByVal e As System.Windows.Forms.MouseEventArgs)

        Public Sub New()
        End Sub

        Public Sub New(ByVal dayOfMonth As Integer)
            Me.dayOfMonth = dayOfMonth
            Me.Size = New Size(75, 75)
            Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
        End Sub

        Private Sub DayControl_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Me.selected = Not Me.selected
                Me.Refresh()
            End If
        End Sub

        Private Sub DayControl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
            If e.Button = Windows.Forms.MouseButtons.Left Then
                RaiseEvent Dragging(Me, e)
            End If
        End Sub

        Private Sub day_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            If selected Then
                e.Graphics.FillRectangle(Brushes.Yellow, Me.ClientRectangle)
            End If
            e.Graphics.DrawString(Day, Me.Font, Brushes.Black, 0, 0)
        End Sub

    End Class

End Class
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
unholly_pluginAuthor Commented:
Thanks Alot,

I havent had a chance yet to really dive into your example code
but loading it in a project it looked just like what i needed.

Of course ill need to make some changes to fit it into my code
but i think you really nailed it! :)

Thanks again.
Yoav.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
No problem.

Let me know if you need help applying the concept to your code.

This is a "grey area" of OOP...it's not obvious how to do this at first as the process is started at a "low level" in the DayControl but has to invovle a "higher control" to achieve the end result.

In my example, the "source" DayControl has no idea what other DayControls have been affected by it...
0
unholly_pluginAuthor Commented:
Of course,

I also believe that this is the way it should be done.
From the application point of view, the Calendar control is the entry point to all the functionality.
of course the day and month controls need to provide information and specific functionality of their
own but its the Calendar object that needs to tie it all together.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.