8

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

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

4

4 回答 4

9

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

可以所有者绘制控件,但这需要太多的工作来证明。这将使您负责自己绘制整个控件。请注意,如果您选择走这条路线,则MonthCalendar控件不会引发Paint事件,因为基本控件将该UserPaint位设置为“False”。您将不得不对控件进行子类化并改写其OnPrint方法

我个人不能推荐任何提供这种级别的自定义的第三方控件,但是快速的谷歌搜索确实出现了一些选项:

于 2011-02-19T06:48:29.017 回答
0

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

转到属性。

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

于 2011-02-19T04:46:05.363 回答
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)

于 2016-06-02T10:46:29.790 回答
-4

Step 1:将grid view控件和日历拖拽到web表单或者window表单上:

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

    }
于 2011-02-19T09:43:21.450 回答