Solved

Multiselecting user controls

Posted on 2007-03-28
4
187 Views
Last Modified: 2013-11-26
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
0
Comment
Question by:unholly_plugin
  • 2
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 18809275
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
 

Author Comment

by:unholly_plugin
ID: 18837088
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18837166
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
 

Author Comment

by:unholly_plugin
ID: 18858087
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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question