Click here to Skip to main content
14,389,126 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to set up backcolor of specific date on MonthCalendar in VB.net!

But what I have tried is still not working:
1. compiler dont know 'DateItem'
2. 'AddDateInfo' is not member of 'MonthCalendar'

What I have tried:

that is C# sample! Another Month Calendar[^]

Form1.Designer.vb
<global.microsoft.visualbasic.compilerservices.designergenerated()>
Partial Class Form1
Inherits System.Windows.Forms.Form

Private Sub InitializeComponent()
Me.MonthCalendar1 = New System.Windows.Forms.MonthCalendar()
Me.SuspendLayout()
'
'MonthCalendar1
'
Me.MonthCalendar1.CalendarDimensions = New System.Drawing.Size(4, 3)
Me.MonthCalendar1.Location = New System.Drawing.Point(18, 18)
Me.MonthCalendar1.Name = "MonthCalendar1"
Me.MonthCalendar1.TabIndex = 0
Me.MonthCalendar1.BackColor = Color.Red
Me.BackColor = Color.Blue

'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(845, 497)
Me.Controls.Add(Me.MonthCalendar1)
Me.Name = "Form1"
Me.Text = "MonthCalendar"
Me.ResumeLayout(False)

End Sub

Friend WithEvents MonthCalendar1 As MonthCalendar

End Class


Form1.vb

Imports System

Public Class Form1
Inherits System.Windows.Forms.Form

Dim m_dateItemCollection As DateItemCollection

Public Sub New()
InitializeComponent()

m_dateItemCollection = New DateItemCollection()

FormatDates()


End Sub

Private Sub FormatDates()
Dim d = New DateItem(4) {}
d.Initialize()

For i = 0 To 5 - 1
d(i) = New DateItem
Next

d(0).DateTime = New DateTime(2019, 11, 3)
d(0).BackColor1 = Color.Red

d(1).DateTime = New DateTime(2019, 11, 12)
d(1).BackColor1 = Color.Red

d(2).DateTime = New DateTime(2019, 11, 16)
d(2).BackColor1 = Color.Red

d(3).DateTime = New DateTime(2019, 11, 18)
d(3).BackColor1 = Color.Red

d(4).DateTime = New DateTime(2019, 11, 22)
d(4).BackColor1 = Color.Red

Me.AddDateInfo(d)

End Sub

Public ReadOnly Property Dates() As DateItemCollection
Get
Return Me.m_dateItemCollection
End Get
End Property



Public Sub AddDateInfo(info As DateItem())
Dim i As Integer
For i = 0 To info.Length - 1 Step i + 1 ' info.Length - 1
If (info(i).BackColor1 <> Nothing) Then
Dates.Add(info(i))
End If
Next

End Sub

Public Sub AddDateInfo(info As DateItem)
Dates.Add(info)
End Sub

'Public Shared Widening Operator CType(v As System.Windows.Forms.MonthCalendar) As MonthCalendar
'Throw New NotImplementedException()
'End Operator
End Class


DateItemCollection.vb

Imports System
Imports System.ComponentModel
Imports System.Windows
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.Collections
Public Class DateItemCollection
Inherits CollectionBase

Private owner As MonthCalendar

'Public Sub New(owner As MonthCalendar) ' MyBase.New()

'If (owner Is Nothing) Then
'Throw New ArgumentNullException("owner")
'End If

'Me.owner = owner
'End Sub

'Public Sub New(owner As MonthCalendar, dateItems As DateItem()) 'Me(owner)

'Me.Add(dateItems)

'End Sub

Public Sub Add(value As DateItem)
Dim index As Integer
If (value Is Nothing) Then
Throw New ArgumentNullException("value")
End If

'If (CType(value.Calendar Is Nothing, MonthCalendar)) Then
'value.Calendar = Me.owner
'End If

index = Me.IndexOf(value)
If (index = -1) Then
Me.List.Add(value)
Else
Me.List(index) = value
End If

End Sub

Public Sub Add(dateItems As DateItem())
If (dateItems Is Nothing) Then
Throw New ArgumentNullException("dateItems")
End If
Dim i As Integer
For i = 0 To dateItems.Count - 1 Step i + 1
Me.Add(dateItems(i))
Next
End Sub


Public Function IndexOf(dateItem As DateItem) As Integer
If (dateItem Is Nothing) Then
Throw New ArgumentNullException("dateItem")
End If

Dim i As Integer
For i = 0 To Me.Count - 1 Step i + 1
If (Me(i).GetType() Is dateItem.GetType()) Then
Return i
End If
Next

Return -1
End Function
</pr
Default Public Overridable ReadOnly Property Item(index As Integer) As DateItem
Get
Return Me.List(index)
End Get
End Property

End Class


DateItem.vb

Imports System
Imports System.ComponentModel
Imports System.Windows
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.Drawing
Imports WindowsApplication2

Public Class DateItem

Private m_date As DateTime
Private m_rangeDate As DateTime

Private disposed As Boolean
Private m_backColor1 As Color
Private m_backColor2 As Color
Private m_calendar As MonthCalendar

Public Sub DateItem()
m_backColor1 = Color.Empty
m_backColor2 = Color.White
End Sub

' add Datetime
Public Property DateTime() As DateTime
Get
Return m_date
End Get
Set(ByVal Value As DateTime)
m_date = Value
m_rangeDate = m_date
End Set
End Property

' add BackColor1
Public Property BackColor1() As Color
Get
Return m_backColor1
End Get
Set(ByVal Value As Color)
m_backColor1 = Value
End Set
End Property

' add BackColor2
Public Property BackColor2() As Color
Get
Return m_backColor2
End Get
Set(ByVal Value As Color)
m_backColor2 = Value
End Set
End Property

' add calendar
Friend Property Calendar() As MonthCalendar
Get
Return m_calendar
End Get
Set(ByVal Value As MonthCalendar)
m_calendar = Value
End Set
End Property


End Class
Posted
Updated 6-Nov-19 19:27pm
v4
Rate this:
Please Sign up or sign in to vote.

Solution 1

VB.NET code:
Private Sub FormatDates()
    Dim d = New DateItem(4) {}
    d.Initialize

    For i = 0 To 5 - 1
        d(i) = New DateItem
    Next

    d(0).Date = New DateTime(2005, 6, 3)
    d(0).BackColor1 = Color.Red
    d(0).ImageListIndex = 3
    d(0).Text = "Help"
    d(1).Date = New DateTime(2005, 6, 12)
    d(1).ImageListIndex = 2
    d(2).Date = New DateTime(2005, 6, 16)
    d(2).BackColor1 = Color.LightBlue
    d(2).ImageListIndex = 8
    d(3).Date = New DateTime(2005, 6, 18)
    d(3).BackColor1 = Color.GreenYellow
    d(3).ImageListIndex = 1
    d(3).Text = "NorDev"
    d(4).Date = New DateTime(2005, 6, 22)
    d(4).ImageListIndex = 1
    d(4).Text = "Cebit"
    monthCalendar1.AddDateInfo(d)
End Sub

C# code:
private void FormatDates()
{
    DateItem[] d = new DateItem[5]; 
    d.Initialize(); 
    for (int i = 0;i<5;i++)
        d[i] = new DateItem();

    d[0].Date = new DateTime(2005,6,3);
    d[0].BackColor1 = Color.Red;
    d[0].ImageListIndex = 3;
    d[0].Text = "Help";
    d[1].Date = new DateTime(2005,6,12);
    d[1].ImageListIndex = 2;
    d[2].Date = new DateTime(2005,6,16);
    d[2].BackColor1 = Color.LightBlue;
    d[2].ImageListIndex = 8;
    d[3].Date = new DateTime(2005,6,18);
    d[3].BackColor1 = Color.GreenYellow;
    d[3].ImageListIndex = 1;
    d[3].Text = "NorDev";
    d[4].Date = new DateTime(2005,6,22);
    d[4].ImageListIndex = 1;
    d[4].Text = "Cebit";

    monthCalendar1.AddDateInfo(d);
}


Don't forget to add the month calendar as a reference in the project!
   
v3
Comments
Maciej Los 31-Oct-19 4:55am
   
Seems OP is expecting VB.NET code ;)
Rate this:
Please Sign up or sign in to vote.

Solution 2

Here is the corrected code for Form1, note the Imports Pabo.Calendar and the Sub FormatDates() changes.
Also note I did not use your other classes, just Form1 and the MonthCalendar dll:
Imports System
Imports Pabo.Calendar

Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim m_dateItemCollection As DateItemCollection

    Public Sub New()
        InitializeComponent()

        m_dateItemCollection = New DateItemCollection(MonthCalendar1)

        FormatDates()
    End Sub

    Private Sub FormatDates()
        Dim d = New DateItem(4) {}
        d.Initialize()

        For i = 0 To 5 - 1
            d(i) = New DateItem
        Next

        d(0).Date = New DateTime(2019, 11, 3)
        d(0).BackColor1 = Color.Red

        d(1).Date = New DateTime(2019, 11, 12)
        d(1).BackColor1 = Color.Red
        d(1).BackColor2 = Color.Beige
        d(1).GradientMode = mcGradientMode.Horizontal

        d(2).Date = New DateTime(2019, 11, 16)
        d(2).BackColor1 = Color.LightCyan

        d(3).Date = New DateTime(2019, 11, 18)
        d(3).BackColor1 = Color.Yellow

        d(4).Date = New DateTime(2019, 11, 22)
        d(4).BackColor1 = Color.LightGreen

        Me.AddDateInfo(d)
        Me.MonthCalendar1.AddDateInfo(d)
    End Sub

    Public ReadOnly Property Dates() As DateItemCollection
        Get
            Return Me.m_dateItemCollection
        End Get
    End Property

    Public Sub AddDateInfo(info As DateItem())
        Dim i As Integer

        For i = 0 To info.Length - 1 Step i + 1 ' info.Length - 1
            If (info(i).BackColor1 <> Nothing) Then
                Dates.Add(info(i))
            End If
        Next
    End Sub
End Class
   
v5
Comments
hawk0006 6-Nov-19 3:26am
   
Thanks for your patient first and updating my question(my first question)!
*I added MonthCalendar.dll.
When changing 'DateTime' to 'Date' in 'DateItem' class,'Date' keyword can not be used.
RickZeeland 6-Nov-19 3:36am
   
You can delete your property as my code uses the DateItem.Date property from the MonthCalendar :)
RickZeeland 6-Nov-19 3:40am
   
It's not a good idea to create your own DateItem class, as the MonthCalendar already has this class :)
hawk0006 6-Nov-19 3:47am
   
yeah, bad idea.

i will try again !
hawk0006 6-Nov-19 4:07am
   
that would be the last question :)

MonthCalendar1 that i used in Form Design -> System.Windows.Forms.MonthCalendar.
In Code -> Pabo.Calendar.MonthCalendar().

so, the message still appear 'AddDateInfo' is not member of 'MonthCalendar'.

thanks you @RickZeeland
RickZeeland 6-Nov-19 4:15am
   
You should use:
Me.MonthCalendar1.AddDateInfo(d)
hawk0006 6-Nov-19 4:20am
   
It is working !
Thanks you @RickZeeland <3
RickZeeland 6-Nov-19 4:25am
   
Did you modify MonthCalendar.cs ?
If so you can better start with a new copy ...
hawk0006 6-Nov-19 4:31am
   
yeah fine with new one, thanks you!
RickZeeland 6-Nov-19 4:31am
   
Phew :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100