MonthCalendar (Clase)
Actualización: noviembre 2007
Representa un control de Windows que permite al usuario seleccionar una fecha mediante una presentación del calendario mensual visual.
Ensamblado: System.Windows.Forms (en System.Windows.Forms.dll)
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] [DefaultBindingPropertyAttribute("SelectionRange")] public class MonthCalendar : Control
/** @attribute ComVisibleAttribute(true) */
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */
/** @attribute DefaultBindingPropertyAttribute("SelectionRange") */
public class MonthCalendar extends Control
El control MonthCalendar permite al usuario seleccionar una fecha mediante una presentación visual. Es posible limitar las fechas y horas que se pueden seleccionar estableciendo las propiedades MinDate y MaxDate.
Para cambiar la presentación de la parte del control que corresponde al calendario, establezca las propiedades ForeColor, Font, TitleBackColor, TitleForeColor, TrailingForeColor y BackColor.
El control MonthCalendar lo dibuja el sistema operativo o el propietario, por lo que nunca se produce el evento Paint. Si necesita dar una apariencia personalizada al control MonthCalendar, debe reemplazar el método OnPrint, llamar a la implementación base de OnPrinty, a continuación, realizar el dibujo personalizado.
Si se necesita aplicar un formato de fecha personalizado y limitar la selección a una sola fecha, se puede utilizar un control DateTimePicker en lugar de un MonthCalendar. El uso del DateTimePicker elimina en gran medida la necesidad de validar los valores de fecha y hora.
Para obtener más información sobre controles del calendario mensual, vea el tema "Controles de calendario mensual" en la documentación de la plataforma SDK en http://msdn2.microsoft.com/es-es/default.aspx.
Nota:
|
|---|
|
El uso de MonthCalendar con los estilos visuales habilitados hace que un intervalo de selección para el control MonthCalendar no se pinte correctamente y que la fecha del día esté dentro de un cuadrado si ShowTodayCircle se ha establecido como true. |
En el ejemplo de código siguiente se muestra un formulario que contiene un control MonthCalendar que muestra un año del calendario. En el ejemplo se muestra cómo establecer propiedades como BackColor, ForeColor, TitleBackColor, TitleForeColor, CalendarDimensions y TrailingForeColor para personalizar la apariencia del control de calendario. Se establecen otras propiedades, como AnnuallyBoldedDates, BoldedDates y MonthlyBoldedDates, para personalizar las fechas que van a aparecer en negrita. En el ejemplo también se establecen las propiedades FirstDayOfWeek, MaxDate, MinDate y MaxSelectionCount para modificar el formato del calendario. También se controlan los eventos DateSelected y DateChanged y su estado se muestra en el formulario.
using System; using System.Drawing; using System.Windows.Forms; public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.MonthCalendar monthCalendar1; private System.Windows.Forms.TextBox textBox1; [STAThread] static void Main() { Application.Run(new Form1()); } public Form1() { this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.textBox1.Location = new System.Drawing.Point(48, 488); this.textBox1.Multiline = true; this.textBox1.ReadOnly = true; this.textBox1.Size = new System.Drawing.Size(824, 32); // Create the calendar. this.monthCalendar1 = new System.Windows.Forms.MonthCalendar(); // Set the calendar location. this.monthCalendar1.Location = new System.Drawing.Point(47, 16); // Change the color. this.monthCalendar1.BackColor = System.Drawing.SystemColors.Info; this.monthCalendar1.ForeColor = System.Drawing.Color.FromArgb( ((System.Byte)(192)), ((System.Byte)(0)), ((System.Byte)(192))); this.monthCalendar1.TitleBackColor = System.Drawing.Color.Purple; this.monthCalendar1.TitleForeColor = System.Drawing.Color.Yellow; this.monthCalendar1.TrailingForeColor = System.Drawing.Color.FromArgb( ((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(0))); // Add dates to the AnnuallyBoldedDates array. this.monthCalendar1.AnnuallyBoldedDates = new System.DateTime[] { new System.DateTime(2002, 4, 20, 0, 0, 0, 0), new System.DateTime(2002, 4, 28, 0, 0, 0, 0), new System.DateTime(2002, 5, 5, 0, 0, 0, 0), new System.DateTime(2002, 7, 4, 0, 0, 0, 0), new System.DateTime(2002, 12, 15, 0, 0, 0, 0), new System.DateTime(2002, 12, 18, 0, 0, 0, 0)}; // Add dates to BoldedDates array. this.monthCalendar1.BoldedDates = new System.DateTime[] {new System.DateTime(2002, 9, 26, 0, 0, 0, 0)}; // Add dates to MonthlyBoldedDates array. this.monthCalendar1.MonthlyBoldedDates = new System.DateTime[] {new System.DateTime(2002, 1, 15, 0, 0, 0, 0), new System.DateTime(2002, 1, 30, 0, 0, 0, 0)}; // Configure the calendar to display 3 rows by 4 columns of months. this.monthCalendar1.CalendarDimensions = new System.Drawing.Size(4, 3); // Set week to begin on Monday. this.monthCalendar1.FirstDayOfWeek = System.Windows.Forms.Day.Monday; // Set the maximum visible date on the calendar to 12/31/2010. this.monthCalendar1.MaxDate = new System.DateTime(2010, 12, 31, 0, 0, 0, 0); // Set the minimum visible date on calendar to 12/31/2010. this.monthCalendar1.MinDate = new System.DateTime(1999, 1, 1, 0, 0, 0, 0); // Only allow 21 days to be selected at the same time. this.monthCalendar1.MaxSelectionCount = 21; // Set the calendar to move one month at a time when navigating using the arrows. this.monthCalendar1.ScrollChange = 1; // Do not show the "Today" banner. this.monthCalendar1.ShowToday = false; // Do not circle today's date. this.monthCalendar1.ShowTodayCircle = false; // Show the week numbers to the left of each week. this.monthCalendar1.ShowWeekNumbers = true; // Add event handlers for the DateSelected and DateChanged events this.monthCalendar1.DateSelected += new System.Windows.Forms.DateRangeEventHandler(this.monthCalendar1_DateSelected); this.monthCalendar1.DateChanged += new System.Windows.Forms.DateRangeEventHandler(this.monthCalendar1_DateChanged); // Set up how the form should be displayed and add the controls to the form. this.ClientSize = new System.Drawing.Size(920, 566); this.Controls.AddRange(new System.Windows.Forms.Control[] {this.textBox1, this.monthCalendar1}); this.Text = "Month Calendar Example"; } private void monthCalendar1_DateSelected(object sender, System.Windows.Forms.DateRangeEventArgs e) { // Show the start and end dates in the text box. this.textBox1.Text = "Date Selected: Start = " + e.Start.ToShortDateString() + " : End = " + e.End.ToShortDateString(); } private void monthCalendar1_DateChanged(object sender, System.Windows.Forms.DateRangeEventArgs e) { // Show the start and end dates in the text box. this.textBox1.Text = "Date Changed: Start = " + e.Start.ToShortDateString() + " : End = " + e.End.ToShortDateString(); } }
import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;
public class Form1 extends System.Windows.Forms.Form
{
private System.Windows.Forms.MonthCalendar monthCalendar1;
private System.Windows.Forms.TextBox textBox1;
/** @attribute STAThread()
*/
public static void main(String[] args)
{
Application.Run(new Form1());
} //main
public Form1()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox1.set_BorderStyle(
System.Windows.Forms.BorderStyle.FixedSingle);
this.textBox1.set_Location(new System.Drawing.Point(48, 488));
this.textBox1.set_Multiline(true);
this.textBox1.set_ReadOnly(true);
this.textBox1.set_Size(new System.Drawing.Size(824, 32));
// Create the calendar.
this.monthCalendar1 = new System.Windows.Forms.MonthCalendar();
// Set the calendar location.
this.monthCalendar1.set_Location(new System.Drawing.Point(47, 16));
// Change the color.
this.monthCalendar1.set_BackColor(
System.Drawing.SystemColors.get_Info());
this.monthCalendar1.set_ForeColor(
System.Drawing.Color.FromArgb(192, 0, 192));
this.monthCalendar1.set_TitleBackColor(System.Drawing.Color.get_Purple());
this.monthCalendar1.set_TitleForeColor(System.Drawing.Color.get_Yellow());
this.monthCalendar1.set_TrailingForeColor(
System.Drawing.Color.FromArgb(192, 192, 0));
// Add dates to the AnnuallyBoldedDates array.
this.monthCalendar1.set_AnnuallyBoldedDates(new System.DateTime[] {
new System.DateTime(2002, 4, 20, 0, 0, 0, 0),
new System.DateTime(2002, 4, 28, 0, 0, 0, 0),
new System.DateTime(2002, 5, 5, 0, 0, 0, 0),
new System.DateTime(2002, 7, 4, 0, 0, 0, 0),
new System.DateTime(2002, 12, 15, 0, 0, 0, 0),
new System.DateTime(2002, 12, 18, 0, 0, 0, 0) });
// Add dates to BoldedDates array.
this.monthCalendar1.set_BoldedDates(new System.DateTime[] {
new System.DateTime(2002, 9, 26, 0, 0, 0, 0) });
// Add dates to MonthlyBoldedDates array.
this.monthCalendar1.set_MonthlyBoldedDates(new System.DateTime[] {
new System.DateTime(2002, 1, 15, 0, 0, 0, 0),
new System.DateTime(2002, 1, 30, 0, 0, 0, 0) });
// Configure the calendar to display 3 rows by 4 columns of months.
this.monthCalendar1.set_CalendarDimensions(new System.Drawing.Size(4, 3));
// Set week to begin on Monday.
this.monthCalendar1.set_FirstDayOfWeek(System.Windows.Forms.Day.Monday);
// Set the maximum visible date on the calendar to 12/31/2010.
this.monthCalendar1.set_MaxDate(
new System.DateTime(2010, 12, 31, 0, 0, 0, 0));
// Set the minimum visible date on calendar to 12/31/2010.
this.monthCalendar1.set_MinDate(
new System.DateTime(1999, 1, 1, 0, 0, 0, 0));
// Only allow 21 days to be selected at the same time.
this.monthCalendar1.set_MaxSelectionCount(21);
// Set the calendar to move one month at a time when navigating
// using the arrows.
this.monthCalendar1.set_ScrollChange(1);
// Do not show the "Today" banner.
this.monthCalendar1.set_ShowToday(false);
// Do not circle today's date.
this.monthCalendar1.set_ShowTodayCircle(false);
// Show the week numbers to the left of each week.
this.monthCalendar1.set_ShowWeekNumbers(true);
// Add event handlers for the DateSelected and DateChanged events
this.monthCalendar1.add_DateSelected(
new System.Windows.Forms.DateRangeEventHandler(
this.monthCalendar1_DateSelected));
this.monthCalendar1.add_DateChanged(
new System.Windows.Forms.DateRangeEventHandler(
this.monthCalendar1_DateChanged));
// Set up how the form should be displayed and add the controls to
// the form.
this.set_ClientSize(new System.Drawing.Size(920, 566));
this.get_Controls().AddRange(new System.Windows.Forms.Control[] {
this.textBox1, this.monthCalendar1 });
this.set_Text("Month Calendar Example");
} //Form1
private void monthCalendar1_DateSelected(Object sender,
System.Windows.Forms.DateRangeEventArgs e)
{
// Show the start and end dates in the text box.
this.textBox1.set_Text("Date Selected: Start = "
+ e.get_Start().ToShortDateString() + " : End = "
+ e.get_End().ToShortDateString());
} //monthCalendar1_DateSelected
private void monthCalendar1_DateChanged(Object sender,
System.Windows.Forms.DateRangeEventArgs e)
{
// Show the start and end dates in the text box.
this.textBox1.set_Text("Date Changed: Start = "
+ e.get_Start().ToShortDateString() + " : End = "
+ e.get_End().ToShortDateString());
} //monthCalendar1_DateChanged
} //Form1
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.MonthCalendar
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile para Pocket PC
.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.
Nota: