Customize calendar C#

Asked

Viewed 1,368 times

3

In the system I’m doing, I need a calendar that has only the weekends of each month enabled to be selected, the rest is deleted..

How can I do this in C# Winforms?

I searched on the Internet and here in the Forum and I found nothing exactly like this, I found some things but I could not do what I want.

  • I didn’t quite understand, it would be like: dom seg ter qua qui sex Sab 1 7 8 14 15 21 22 28 29 , that would be a correct example?

  • the formatting does not catch here, imagine q the month I spoke day 1 is Sunday and it only shows Saturday and Sunday on the calendar.

  • Yes. For example, in January 2015 you will only be able to select the weekends (Saturday and Sunday) as well as the other months. It only enables weekends.

  • is to be displayed in what form? list? table?

  • In the same calendar of Windows Form Monthcalendar. Only then it would be enabled to select only the Saturdays and Sundays of each month.

1 answer

5


Using the MonthCalendar native, the most you can do is check if the date selected is a Saturday or a Sunday, something like that:

private void monthCalendar_DateChanged(object sender, DateRangeEventArgs e)
{
    // se a data selecionada for diferente de domingo e sábado
    if (e.Start.DayOfWeek != DayOfWeek.Sunday && e.Start.DayOfWeek != DayOfWeek.Saturday)
    {
        // seleciona o primeiro domingo anterior a data selecionada
        monthCalendar.SelectionStart = monthCalendar.SelectionStart
            .AddDays(-(Double)e.Start.DayOfWeek);
        monthCalendar.SelectionEnd = monthCalendar.SelectionStart;

        // exibe mensagem de data fora do intervalo
        MessageBox.Show("Você não pode selecionar nenhuma data entre segunda e sexta.", 
            "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
}

If this really doesn’t suit you, you can try using one of the custom controls below:

In the MonthCalendar 1, you have the event MonthDayRender, where you can prevent the days of the week from being created.

private void monthCalendar1_MonthDayRender(object sender, MonthCalendar.MonthDayRenderEventArgs e)
{
    if (e.WeekDay != DayOfWeek.Sunday && e.WeekDay != DayOfWeek.Saturday)
    {
        e.OwnerDraw = true;
    }
}

Something that might also be interesting to you in this control is the property ShowTrailingsDays which is inside the property MonthDays, this property hides the days before and after the current calendar month.

In the MonthCalendar 2, you have the event DayRender, which can also prevent the days of the week from being created.

private void monthCalendar2_DayRender(object sender, Pabo.Calendar.DayRenderEventArgs e)
{
    if (e.Date.DayOfWeek != DayOfWeek.Sunday && e.Date.DayOfWeek != DayOfWeek.Saturday)
    {
        e.OwnerDraw = true;
    }
}

Apparently the MonthCalendar 1 is more complete and enables greater customization.

Observing: I never used any of the two components, I found them after doing some research on the internet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.