Customize calendar C #

3

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

How can I do this in C # Winforms?

I searched the internet and here in the Forum and did not find anything exactly like this, I found some things but I could not do what I want.

    
asked by anonymous 17.12.2014 / 22:15

1 answer

5

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

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 does not really suit you, you can try using one of the custom controls below:

In MonthCalendar , you have event MonthCalendar 1 , 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 may also be of interest to you in this control is the MonthDayRender property that is inside the ShowTrailingsDays property, this property hides the days before and after the current calendar month.

In%%, you have event MonthDays , which can also prevent weekdays 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;
    }
}

It seems that MonthCalendar 2 is more complete and enables more customization.

  

Note: I never got to use either of the two components, I found them after doing some research on the internet.

    
04.01.2015 / 04:09