Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1190
  • Last Modified:

Help extending the monthCalendar control.

I am wanting to extend the MonthCalendar control so that where it has the box/circle showing todays date at the bottom, I would like another itme below that showing the highlighted box and what it's date is(see picture).  I would like to add the line where the red arrow is....

I have gotten started with a little bit of code, but not sure exactly where to go from here. There are currently 2 properties that provide the visibility of the footer at the bottom, the ShowToday as Boolean and ShowTodayCircle as Boolean.

I would expect that on the Paint method it rights the date in the footer if the showTodat is set to true.  Is there a way to see the code for these items.  How do you know what to write for the OnPaint.

Thought this might be fun and challenging, but I have bitten off mor than I can chew I think.
Public Class myCalendar
    Inherits System.Windows.Forms.MonthCalendar
 
    Private _showDate As Boolean
    Public Property ShowDate() As Boolean
        Get
            Return _showDate
 
        End Get
        Set(ByVal value As Boolean)
 
            _showDate = value
        End Set
    End Property
 
    Private _showDateCircle As Boolean
    Public Property ShowDateCircle() As Boolean
        Get
            Return _showDateCircle
        End Get
        Set(ByVal value As Boolean)
 
            _showDateCircle = value
        End Set
       
    End Property

Open in new window

Image1.jpg
0
rgn2121
Asked:
rgn2121
  • 8
  • 3
1 Solution
 
rachitkohliCommented:
Hello,
I have written the code doing your functionality.
1. Make an windows user control project. Drop a Month calendar on it.
2. Exactly below it drop a label, with its AutoSize property as FALSE.
3. Now in the code window, paste the following code.
4. After pasting the code, build the project & use this control on any windows form, you will see it working the way you wanted with an additional item below showing the currently selected date & a blue rectangle next to it.
Hope it helps

    Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
        e.Graphics.FillRectangle(Brushes.Blue, 1, 1, 10, 10)
        e.Graphics.DrawString(MonthCalendar1.SelectionStart.ToLongDateString, New Font("Verdana", 7, FontStyle.Bold), Brushes.Black, 15, 0)
    End Sub
 
    Private Sub MonthCalendar1_DateSelected(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateSelected
        Label1.Refresh()
    End Sub

Open in new window

calendar-ex.JPG
0
 
rgn2121Author Commented:
That's great!!! Thanks...

Would I be able to make the Blue box appear the same shape and size as the Red outline box above it and also have the area where it is displayed as whitesmoke so it appears as though it is part of the control?
0
 
rachitkohliCommented:
Yes for that matter need to get the exact size of the red outline box.

Replace the paint event code with this & change the labels backcolor to white or whitesmoke, whichever you find good
Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
        e.Graphics.FillRectangle(Brushes.Blue, 1, 1, 20, 12)
        e.Graphics.DrawString(MonthCalendar1.SelectionStart.ToLongDateString, New Font("Verdana", 7, FontStyle.Bold), Brushes.Black, 22, 0)
    End Sub

Open in new window

0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
rgn2121Author Commented:
I will try it out later tonight and let you know...thanks
0
 
rgn2121Author Commented:
Okay, the calendar works and displays as expected, but I don't have all of the properties that a normal calendar would have.  I assume this is because it inherits from UserControl and not MonthCalendar?

If I change it to monthCalendar though, I get errors about Auto size not being a member of MonthCalendar.  Anyway for me to have all of the properties of a normal calendar.  I have been able to create my own and they show up, surely I don't have to create all the ones that are missing do I?
0
 
rgn2121Author Commented:
This is the code I have right now...I was trying to make the new properties I added show on the control when they are changed, but I am missing something.  Maybe I need to create a method to paint the label when they are updated?
Imports System.ComponentModel
Public Class MonthCalendarExt
 
 
    Private Sub labelCalendar_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles labelCalendar.Paint
        e.Graphics.FillRectangle(Brushes.Blue, 1, 1, 23, 14)
        e.Graphics.DrawString(_extraDate & MonthCalendar1.SelectionStart.ToShortDateString, New Font("Microsoft Sans Serif", 9, FontStyle.Bold), Brushes.Black, 24, 0)
    End Sub
 
    Private Sub MonthCalendar1_DateSelected(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateSelected
        labelCalendar.Refresh()
    End Sub
 
    Private _extraDate As String
    <Category("Behavior.ExtraDate"), _
        Description("Text diplayed as a description of the extra date."), _
        DefaultValue("NewDate")> _
    Public Property ExtraDate() As String
        Get
            Return _extraDate
        End Get
        Set(ByVal value As String)
            _extraDate = value
 
            Me.Invalidate()
        End Set
    End Property
 
    Private _showExtraDate As Boolean
    <Category("Behavior.ExtraDate"), _
        Description("Set to true to show the selected date under the Today's Date."), _
        DefaultValue(False)> _
    Public Property ShowExtraDate() As Boolean
        Get
            Return _showExtraDate
        End Get
        Set(ByVal value As Boolean)
            _showExtraDate = value
            Me.Invalidate()
        End Set
    End Property

Open in new window

0
 
rachitkohliCommented:
On Paint Event write labelCalendar.Refresh()
0
 
rgn2121Author Commented:
What about the Calendar properties...do I need to inherit from it instead...?
"Okay, the calendar works and displays as expected, but I don't have all of the properties that a normal calendar would have.  I assume this is because it inherits from UserControl and not MonthCalendar?

If I change it to monthCalendar though, I get errors about Auto size not being a member of MonthCalendar.  Anyway for me to have all of the properties of a normal calendar.  I have been able to create my own and they show up, surely I don't have to create all the ones that are missing do I?"
0
 
rgn2121Author Commented:
when I added the labelCalendar.refresh() to the OnPaint, I no longer can see my properties in the property window.
0
 
rgn2121Author Commented:
???
0
 
rgn2121Author Commented:
Any help with what I posted above?
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 8
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now