【发布时间】:2011-06-30 06:00:55
【问题描述】:
如何在 VB.NET 中更改 MonthCalendar 控件中某些日期的颜色?
例如,我需要将 1 月 21 日的颜色改为红色,星期天改为橙色等等...
【问题讨论】:
-
你的 Visual Studio 版本是什么? 2005 年或 2008 年或 2010 年。我可以假设月历适用于 winform 吗?
标签: .net vb.net winforms monthcalendar
如何在 VB.NET 中更改 MonthCalendar 控件中某些日期的颜色?
例如,我需要将 1 月 21 日的颜色改为红色,星期天改为橙色等等...
【问题讨论】:
标签: .net vb.net winforms monthcalendar
这是不可能的。没有内置方法可以自定义在 MonthCalendar 控件上显示各个日期或日期的方式。
您可以自行绘制控件,但这样做的工作量太大了。这将使您负责自己绘制整个控件。请注意,如果您选择走这条路线,MonthCalendar 控件不会引发Paint 事件,因为基本控件将UserPaint 位设置为“False”。您必须将控件子类化并覆盖其OnPrint method。
我个人不能推荐任何提供这种自定义级别的第三方控件,但快速的 Google 搜索似乎会出现一些选项:
【讨论】:
试试这个:
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)
【讨论】:
在 Visual Studio 2005 中,您可以从工具箱中拖动月历。
转到属性。
有每年粗体日期、每月粗体日期和粗体日期。您可以在这些属性中添加所需的日期。
【讨论】:
第一步:在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();
}
}
【讨论】: