How can I change the color of certain dates in the MonthCalendar control?
How can I change the color of certain dates in the Mo开发者_JAVA技巧nthCalendar control in VB.NET?
For example, I need to change the color of Jan 21 to Red, Sundays to Orange and so on...
This is not possible. There is no built-in way of customizing the way that individual days or dates are displayed on the MonthCalendar
control.
You could owner-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 MonthCalendar
control does not raise the Paint
event because the base control sets the UserPaint
bit to "False". You will have to subclass the control and override its OnPrint
method instead.
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:
- Another Month Calendar
- Customizable MonthCalendar Type Control
- MonthCalendar Calendar - Control
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)
In Visual Studio 2005, you drag a monthcalendar from the toolbox.
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.
Step 1: Drag grid view Control and calender on the web form or window form:
step 2: paste the coding on .cs page
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();
}
}
精彩评论