Как настроить backgroud цвет monthcalendar даты в VB.NET-что?
Я хочу, чтобы установить цвет фона конкретную дату на MonthCalendar в VB.net!
Но то, что я пробовал, все равно не работает:
1. компилятор не знает 'DateItem'
2. 'AddDateInfo' не является членом 'MonthCalendar'
Что я уже пробовал:
это и есть образец C#! Еще Один Месяц Календаря[^]
Форма form1.Дизайнер.ВБ
<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
Форма 1.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.ВБ
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