如何更改MonthCalendar控件中某些日期的颜色?

时间:2011-02-19 04:36:24

标签: .net vb.net winforms monthcalendar

如何在VB.NET中更改MonthCalendar控件中某些日期的颜色?

例如,我需要将1月21日的颜色更改为红色,星期日更改为橙色等等......

4 个答案:

答案 0 :(得分:9)

这是不可能的。没有内置的方式来自定义MonthCalendar控件上显示单个日期或日期的方式。

可以所有者 - 绘制控件,但这太合理了。这将使您自己负责绘制整个控件。请注意,如果选择此路由,MonthCalendar控件不会引发Paint事件,因为基本控件将UserPaint位设置为“False”。您必须对控件进行子类化并覆盖其OnPrint method

我无法亲自推荐任何提供此级别自定义功能的第三方控件,但快速Google搜索确实会出现一些选项:

答案 1 :(得分:0)

在Visual Studio 2005中,您可以从工具箱中拖动月份日历。

转到属性。

每年都有加粗日期,每月加粗日期和加粗日期。您可以在这些属性中添加所需的日期。

答案 2 :(得分:0)

试试这个:

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

此子画面在一个特定日期(数据)中用一种颜色(cor)绘制一个MonthCalendar(mc)

答案 3 :(得分:-3)

步骤1:在网络表单或窗口表单上拖动网格视图控件和日历:

第2步:在.cs页面上粘贴编码

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();
        }

    }
相关问题