【问题标题】:How can I change the color of certain dates in the MonthCalendar control?如何更改 MonthCalendar 控件中某些日期的颜色?
【发布时间】:2011-06-30 06:00:55
【问题描述】:

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

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

【问题讨论】:

  • 你的 Visual Studio 版本是什么? 2005 年或 2008 年或 2010 年。我可以假设月历适用于 winform 吗?

标签: .net vb.net winforms monthcalendar


【解决方案1】:

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

可以自行绘制控件,但这样做的工作量太大了。这将使您负责自己绘制整个控件。请注意,如果您选择走这条路线,MonthCalendar 控件不会引发Paint 事件,因为基本控件将UserPaint 位设置为“False”。您必须将控件子类化并覆盖其OnPrint method

我个人不能推荐任何提供这种自定义级别的第三方控件,但快速的 Google 搜索似乎会出现一些选项:

【讨论】:

  • 是否有任何第三方组件可以让工作变得轻松?
  • @abcd:我不知道也不能亲自推荐。我已经更新了我的答案,其中一些快速谷歌搜索出现了。
  • 这些都是 C#。有谁知道任何 VB.net 控件?
  • @Urbycoz 控件的语言无关紧要。只需从您的解决方案中引用 DLL。或者,如果您想访问源代码,请将新的 C# 项目添加到现有解决方案并引用它构建的 DLL。
【解决方案2】:

试试这个:

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】:

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

    转到属性。

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

    【讨论】:

    • 这只会使日期变粗,我想更改某些特定日期的颜色
    • 我知道这对于网络日历是可能的。但我不确定 2005 年的 Winform。为什么不尝试 2010 年。2010 年 C# 有更多功能
    • 这不是 C# 功能,而是 WinForms 功能。它肯定不是 VS 2010 (.NET 4.0) 添加的。
    • 对不起。我只是猜测。我还没用过2010。你的回答很吸引人。 Tkz..
    【解决方案4】:

    第一步:在web表单或窗口表单上拖动grid view控件和日历:

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

    【讨论】:

    • 你能解释一下你的代码吗?我不知道它是什么意思
    猜你喜欢
    • 1970-01-01
    • 2016-03-31
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 2019-10-01
    • 1970-01-01
    相关资源
    最近更新 更多