Csharp/CSharp Tutorial/GUI Windows Forms/MonthCalendar
DataTime BoldedDates
<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public partial class Form1 : Form {
public Form1() { InitializeComponent(); } private void addButton_Click(object sender, EventArgs e) { System.DateTime[] boldDates = new System.DateTime[monthCalendar.BoldedDates.Length + 1]; int i =0; foreach (DateTime currentDate in monthCalendar.BoldedDates) { boldDates[i] = currentDate; i++; } boldDates[i] = System.DateTime.Parse(specialDate.Text); monthCalendar.BoldedDates = boldDates; } private void monthCalendar_DateSelected(object sender, DateRangeEventArgs e) { specialDate.Text = e.Start.ToLongDateString(); }
} partial class Form1 {
private void InitializeComponent() { System.Windows.Forms.Label label1; System.Windows.Forms.Button addButton; this.monthCalendar = new System.Windows.Forms.MonthCalendar(); this.specialDate = new System.Windows.Forms.DateTimePicker(); label1 = new System.Windows.Forms.Label(); addButton = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // label1.AutoSize = true; label1.Location = new System.Drawing.Point(12, 16); label1.Name = "label1"; label1.Size = new System.Drawing.Size(90, 13); label1.TabIndex = 2; label1.Text = "Pick a date to add"; // // addButton // addButton.Location = new System.Drawing.Point(289, 9); addButton.Name = "addButton"; addButton.Size = new System.Drawing.Size(75, 23); addButton.TabIndex = 3; addButton.Text = "Add"; addButton.Click += new System.EventHandler(this.addButton_Click); // // monthCalendar // this.monthCalendar.CalendarDimensions = new System.Drawing.Size(3, 3); this.monthCalendar.Location = new System.Drawing.Point(18, 74); this.monthCalendar.Name = "monthCalendar"; this.monthCalendar.Size = new System.Drawing.Size(542, 439); this.monthCalendar.TabIndex = 0; this.monthCalendar.DateSelected += new System.Windows.Forms.DateRangeEventHandler(this.monthCalendar_DateSelected); // // specialDate // this.specialDate.Location = new System.Drawing.Point(108, 12); this.specialDate.Name = "specialDate"; this.specialDate.Size = new System.Drawing.Size(175, 20); this.specialDate.TabIndex = 1; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(573, 529); this.Controls.Add(addButton); this.Controls.Add(label1); this.Controls.Add(this.specialDate); this.Controls.Add(this.monthCalendar); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); this.PerformLayout(); } private System.Windows.Forms.MonthCalendar monthCalendar; private System.Windows.Forms.DateTimePicker specialDate;
} public class DataTimePickerBoldedDates {
[STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); }
}</source>
Get start and end selection on a MonthCalendar
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.MonthCalendar monthCalendar1; private System.Windows.Forms.Button button1; public Form1() { this.monthCalendar1 = new System.Windows.Forms.MonthCalendar(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // monthCalendar1 // this.monthCalendar1.FirstDayOfWeek = System.Windows.Forms.Day.Thursday; this.monthCalendar1.Location = new System.Drawing.Point(16, 16); this.monthCalendar1.Name = "monthCalendar1"; this.monthCalendar1.ShowTodayCircle = false; this.monthCalendar1.ShowWeekNumbers = true; this.monthCalendar1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(48, 184); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(128, 23); this.button1.TabIndex = 1; this.button1.Text = "Selection Range"; this.button1.Click += new System.EventHandler(this.button1_Click_1); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(232, 213); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1, this.monthCalendar1}); this.Text = "Calendar Control"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { string str = monthCalendar1.Text.ToString(); } private void button1_Click(object sender, System.EventArgs e) { } private void button1_Click_1(object sender, System.EventArgs e) { SelectionRange sr = monthCalendar1.SelectionRange; DateTime st = sr.Start; DateTime se = sr.End; MessageBox.Show("RANGE START = " + st.ToString() + "\nRANGE END = " + se.ToString(), "Range Selection"); }
}</source>
Put MonthCalendar On a Form
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class MonthCalendarOnForm : System.Windows.Forms.Form {
private System.Windows.Forms.MonthCalendar monthCalendar1; private System.ruponentModel.Container components = null; public MonthCalendarOnForm() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.monthCalendar1 = new System.Windows.Forms.MonthCalendar(); this.SuspendLayout(); this.monthCalendar1.Location = new System.Drawing.Point(47, 56); this.monthCalendar1.Name = "monthCalendar1"; this.monthCalendar1.TabIndex = 0; this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.monthCalendar1}); this.Name = "MonthCalendarOnForm"; this.Text = "MonthCalendar Control Example"; this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new MonthCalendarOnForm()); }
}</source>