Click here to Skip to main content
14,392,354 members

How to set up backgroud color of monthcalendar of date in VB.NET?

hawk0006 asked:

Open original thread
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
Tags: VB.NET

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




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