.net 如何更改 MonthCalendar 控件中某些日期的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5048872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I change the color of certain dates in the MonthCalendar control?
提问by SpongeBob SquarePants
How can I change the color of certain dates in the MonthCalendar control in VB.NET?
如何在 VB.NET 的 MonthCalendar 控件中更改某些日期的颜色?
For example, I need to change the color of Jan 21 to Red, Sundays to Orange and so on...
例如,我需要将 1 月 21 日的颜色更改为红色,将星期日更改为橙色等等...
回答by Cody Gray
This is not possible.There is no built-in way of customizing the way that individual days or dates are displayed on the MonthCalendarcontrol.
这不可能。没有内置的方式来自定义单个日期或日期在MonthCalendar控件上的显示方式。
You couldowner-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 MonthCalendarcontrol does not raise the Paintevent because the base control sets the UserPaintbit to "False". You will have to subclass the control and override its OnPrintmethodinstead.
您可以自行绘制控件,但这样做的工作量太大而无法证明其合理性。这将使您负责自己绘制整个控件。请注意,如果您选择走这条路线,该MonthCalendar控件不会引发Paint事件,因为基本控件将该UserPaint位设置为“False”。您将不得不子类化控件并重写其OnPrint方法。
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:
我个人不能推荐任何提供这种自定义级别的第三方控件,但快速的 Google 搜索似乎确实提供了一些选项:
回答by william
In Visual Studio 2005, you drag a monthcalendar from the toolbox.
在 Visual Studio 2005 中,您可以从工具箱中拖动月历。
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.
有每年加粗的日期、每月加粗的日期和加粗的日期。您可以在这些属性中添加所需的日期。
回答by Ricardo Rodrigues
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)
此子在一个特定日期(数据)中用一种颜色(cor)绘制一个 MonthCalendar(mc)
回答by Sunny
Step 1: Drag grid view Control and calender on the web form or window form:
第一步:在网页表单或窗口表单上拖动网格视图控件和日历:
step 2: paste the coding on .cs page
第 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();
}
}

