我正在用C#开发学校管理系统。净。我需要通过点击日历中的日期来让用户添加事件,并且添加后日期应该是粗体。我试过下面的代码,但只有最后添加的日期是粗体。但我需要大胆选择每一天。有人可以帮我将所有选定的日期添加到Bolded Days数组吗?我会感激任何帮助。这是我的代码。
//Create method for add bold days
public void boldDays()
{
List<DateTime> l = new List<DateTime>();
l.Add(DateTime.Parse(monthCalendar1.SelectionStart.ToLongDateString()));
DateTime[] s = l.ToArray();
monthCalendar1.BoldedDates = new DateTime[] { };
monthCalendar1.BoldedDates = l.ToArray();
}
//Executing method when a button click
private void btnRefresh_Click(object sender, EventArgs e)
{
boldDays();
}
答案 0 :(得分:0)
从您的代码中,您只添加了一个日期。您需要遍历事件列表。以下代码可能有所帮助:
public void boldDays()
{
List<DateTime> l = new List<DateTime>();
foreach (var event in savedEvents)
{
l.Add(event.Date);
}
monthCalendar1.BoldedDates = l.ToArray();
}
答案 1 :(得分:0)
您只是在数组中添加开始日期。试试这个
public void boldDays()
{
List<DateTime> l = new List<DateTime>();
for(DateTime m = monthCalendar1.SelectionRange.Start;m <= monthCalendar1.SelectionRange.End; m = m.AddDays(1) )
{
l.Add(DateTime.Parse(m.ToLongDateString()));
}
//monthCalendar1.BoldedDates = new DateTime[] { };
l.AddRange(monthCalendar1.BoldedDates); //If you want to preserve previously added dates then add previously added dates to list as
monthCalendar1.BoldedDates = l.ToArray();
}