简体   繁体   中英

How to make all sundays red on month calendar in c#?

我想知道是否有人知道如何在.NET月历上制作所有星期日的背景颜色为红色?

If you want to color individual days in the calendar, then you should take a look at Calendar.SelectedDates and Calendar.SelectedDayStyle properties

Then you could do something like this

myCal.SelectedDates.Add({DateTime object});
myCal.SelectedDayStyle.BackColor = System.Drawing.Color.Red;

This is useful eg when displaying dates with certain events.

If you want to color specific dates in the month, then you should take a look at Calendar.DayRender Event. This event should help you to render every sunday red by doing something like this (Using the DayOfWeek enumeration)

void DayRender(Object source, DayRenderEventArgs e) 
{
  // Change the background color of the days in the month to Red.
  if (e.Day.Date.DayOfWeek == DayOfWeek.Sunday)
     e.Cell.BackColor=System.Drawing.Color.Red;
}

I've done this in ASP.Net by using a per date event that can be used. Just check the day-of-the-week for the current day and if it checks out update the style (or whatever) of the current day.

If your looking at WinForms, I'd assume it would have something similar. I don't remember what the bits are named but that shouldn't be hard to find.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM