How can I change the color of certain dates in the MonthCalendar control?
Asked Answered
W

4

8

How can I change the color of certain dates in the MonthCalendar control in VB.NET?

For example, I need to change the color of Jan 21 to Red, Sundays to Orange and so on...

Wifely answered 19/2, 2011 at 4:36 Comment(1)
Wht's ur visual studio version? 2005 or 2008 or 2010. And Can I assume that monthcalendar is for winform?Dumpcart
O
10

This is not possible. There is no built-in way of customizing the way that individual days or dates are displayed on the MonthCalendar control.

You could owner-draw the control, but that's way too much work to justify. This will make you responsible for drawing the entire control yourself. Note that if you choose to go this route, the MonthCalendar control does not raise the Paint event because the base control sets the UserPaint bit to "False". You will have to subclass the control and override its OnPrint method instead.

I can't personally recommend any third-party controls that provide this level of customization, but a quick Google search does appear to turn up a few options:

Okapi answered 19/2, 2011 at 6:48 Comment(4)
Is there any 3rd party component which would make the job easy ?Wifely
@abcd: Not one that I know of or could personally recommend. I've updated my answer with a few that a quick Google search turns up.Okapi
These are all C#. Does anyone know of any VB.net controls?Adroit
@Adroit It doesn't matter what language the controls are in. Just reference the DLL from your solution. Or, if you want to access the source code, add a new C# project to your existing solution and reference the DLL that it builds.Okapi
D
0

In Visual Studio 2005, you drag a monthcalendar from the toolbox.

Go to the properties.

There's annually bolded dates, monthly bolded dates and bolded dates. You can add the dates you want in those properties.

Dumpcart answered 19/2, 2011 at 4:46 Comment(4)
That will only make the dates bold , I want to change the color of of some particular datesWifely
I know that is possible for web calendar. But I m not sure for Winform in 2005. Why dun u try 2010. There are a lot more features for C# in 2010Dumpcart
This isn't a C# feature, it's a WinForms feature. And it's certainly not one that is added by VS 2010 (.NET 4.0).Okapi
Sry. I m just guessing. I've never used 2010 yet. Your answer is fascinating. Tkz..Dumpcart
S
0

Try this:

Private Sub pintaCalendarioNaData(ByRef mc As MonthCalendar, ByVal data As Date, ByVal cor As String)
        Dim gMonthCalendar As Graphics = mc.CreateGraphics()
        Dim oHTIMonths As MonthCalendar.HitTestInfo
        Dim arrDates As New ArrayList()
        Try
            For intRows As Integer = 1 To mc.Size.Width - 1
                For intCols As Integer = 1 To mc.Size.Height - 1
                    oHTIMonths = mc.HitTest(intRows, intCols)
                    If oHTIMonths.HitArea = MonthCalendar.HitArea.Date Then
                        If CDate(mc.HitTest(intRows, intCols).Time) = CDate(data) Then
                             gMonthCalendar.DrawRectangle(New Pen(ColorTranslator.FromHtml(cor), 2), intRows, intCols, 24, 15)
                            GoTo fim
                        End If
                    End If
                Next intCols
            Next intRows
fim:
        Catch ex As Exception
            MessageBox.Show("Error: " & vbNewLine & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Err.Clear()
        Finally

        End Try
    End Sub

This sub paints one MonthCalendar (mc) in one specific date (data) with one color (cor)

Symphonist answered 2/6, 2016 at 10:46 Comment(0)
P
-3

Step 1: Drag grid view Control and calender on the web form or window form:

step 2: paste the coding on .cs page

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;

public partial class frmCalander : System.Web.UI.Page
{
    SqlConnection con= new SqlConnection();
    SqlDataAdapter myda;
    DataSet ds = new DataSet();
    DataSet dsSelDate;
    String strConn;
    protected void Page_Load(object sender, EventArgs e)
    {
        con.ConnectionString = ConfigurationManager.ConnectionStrings["STUDENTConnectionString"].ConnectionString;
        myda = new SqlDataAdapter("Select * from EventTable", con);
        myda.Fill(ds, "Table");

    }
    protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
    {
        if (! e.Day.IsOtherMonth )
        {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if ((dr["EventDate"].ToString() != DBNull.Value.ToString()))
                    {
                        DateTime dtEvent= (DateTime)dr["EventDate"];
                        if (dtEvent.Equals(e.Day.Date))
                        {
                            e.Cell.BackColor = Color.PaleVioletRed;
                        }
                    }
                }
        }
//If the month is not CurrentMonth then hide the Dates
        else
        {
                e.Cell.Text = "";
        }
    }


    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        myda = new SqlDataAdapter("Select EventId, EventName, EventLocation, Convert(varchar,EventDate,105) as EventDate  from EventTable where EventDate='" + Calendar1.SelectedDate.ToString() + "'", con);
        dsSelDate = new DataSet();
        myda.Fill(dsSelDate, "AllTables");
        if (dsSelDate.Tables[0].Rows.Count == 0)
        {
            GridView1.Visible = false;
        }
        else
        {
            GridView1.Visible = true;
            GridView1.DataSource = dsSelDate;
            GridView1.DataBind();
        }

    }
Pavier answered 19/2, 2011 at 9:43 Comment(1)
Can you explain your Code?, I have no idea what it meansWifely

© 2022 - 2024 — McMap. All rights reserved.