Csharp/C Sharp/GUI Windows Form/DateTimePicker

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

DateTimePicker Format: Short, Time, default and Custom Format

<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 class Form1 : Form {

 private System.Windows.Forms.DateTimePicker dateTimePicker1;
 private System.Windows.Forms.Label label1;
 private System.Windows.Forms.Label label2;
 private System.Windows.Forms.Label label3;
 private System.Windows.Forms.Label label4;
 private System.Windows.Forms.DateTimePicker dateTimePicker2;
 private System.Windows.Forms.DateTimePicker dateTimePicker3;
 private System.Windows.Forms.DateTimePicker dateTimePicker4;
 public Form1() {
       InitializeComponent();
 }
 private void InitializeComponent()
 {
   this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.label3 = new System.Windows.Forms.Label();
   this.label4 = new System.Windows.Forms.Label();
   this.dateTimePicker2 = new System.Windows.Forms.DateTimePicker();
   this.dateTimePicker3 = new System.Windows.Forms.DateTimePicker();
   this.dateTimePicker4 = new System.Windows.Forms.DateTimePicker();
   this.SuspendLayout();
   // 
   // dateTimePicker1
   // 
   this.dateTimePicker1.Location = new System.Drawing.Point(92, 15);
   this.dateTimePicker1.Name = "dateTimePicker1";
   this.dateTimePicker1.Size = new System.Drawing.Size(200, 21);
   this.dateTimePicker1.TabIndex = 0;
   // 
   // label1
   // 
   this.label1.AutoSize = true;
   this.label1.Location = new System.Drawing.Point(12, 19);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(30, 13);
   this.label1.TabIndex = 2;
   this.label1.Text = "Long:";
   // 
   // label2
   // 
   this.label2.AutoSize = true;
   this.label2.Location = new System.Drawing.Point(12, 54);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(33, 13);
   this.label2.TabIndex = 3;
   this.label2.Text = "Short:";
   // 
   // label3
   // 
   this.label3.AutoSize = true;
   this.label3.Location = new System.Drawing.Point(12, 92);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(29, 13);
   this.label3.TabIndex = 4;
   this.label3.Text = "Time:";
   // 
   // label4
   // 
   this.label4.AutoSize = true;
   this.label4.Location = new System.Drawing.Point(12, 128);
   this.label4.Name = "label4";
   this.label4.Size = new System.Drawing.Size(64, 13);
   this.label4.TabIndex = 5;
   this.label4.Text = "Custom ISO:";
   // 
   // dateTimePicker2
   // 
   this.dateTimePicker2.Format = System.Windows.Forms.DateTimePickerFormat.Short;
   this.dateTimePicker2.Location = new System.Drawing.Point(92, 50);
   this.dateTimePicker2.Name = "dateTimePicker2";
   this.dateTimePicker2.Size = new System.Drawing.Size(200, 21);
   this.dateTimePicker2.TabIndex = 6;
   // 
   // dateTimePicker3
   // 
   this.dateTimePicker3.Format = System.Windows.Forms.DateTimePickerFormat.Time;
   this.dateTimePicker3.Location = new System.Drawing.Point(92, 88);
   this.dateTimePicker3.Name = "dateTimePicker3";
   this.dateTimePicker3.Size = new System.Drawing.Size(200, 21);
   this.dateTimePicker3.TabIndex = 7;
   // 
   // dateTimePicker4
   // 
   this.dateTimePicker4.CustomFormat = "yyyy-mm-dd";
   this.dateTimePicker4.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
   this.dateTimePicker4.Location = new System.Drawing.Point(92, 124);
   this.dateTimePicker4.Name = "dateTimePicker4";
   this.dateTimePicker4.Size = new System.Drawing.Size(200, 21);
   this.dateTimePicker4.TabIndex = 8;
   // 
   // Form1
   // 
   this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
   this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
   this.ClientSize = new System.Drawing.Size(348, 192);
   this.Controls.Add(this.dateTimePicker4);
   this.Controls.Add(this.dateTimePicker3);
   this.Controls.Add(this.dateTimePicker2);
   this.Controls.Add(this.label4);
   this.Controls.Add(this.label3);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.dateTimePicker1);
   this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
   this.Name = "Form1";
   this.Text = "Date Controls";
   this.ResumeLayout(false);
   this.PerformLayout();
 }
 [STAThread]
 static void Main()
 {
   Application.EnableVisualStyles();
   Application.Run(new Form1());
 }

}


      </source>


DateTimePicker Value changed event (Selected event)

<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 class Form1 : Form {

   private System.Windows.Forms.DateTimePicker dateTimePickerDropOff;
   private System.Windows.Forms.Label outputLabel;
   private System.Windows.Forms.Label deliveryLabel;
   private System.Windows.Forms.Label dropOffLabel;
   public Form1() {
       InitializeComponent();
       dateTimePickerDropOff.MinDate = DateTime.Today;
       dateTimePickerDropOff.MaxDate = DateTime.Today.AddYears( 1 );
 }
  private void dateTimePickerDropOff_ValueChanged( object sender, EventArgs e )
  {
     DateTime dropOffDate = dateTimePickerDropOff.Value;
     if ( dropOffDate.DayOfWeek == DayOfWeek.Friday ||
        dropOffDate.DayOfWeek == DayOfWeek.Saturday ||
        dropOffDate.DayOfWeek == DayOfWeek.Sunday )
        outputLabel.Text = dropOffDate.AddDays( 3 ).ToLongDateString();
     else
        outputLabel.Text = dropOffDate.AddDays( 2 ).ToLongDateString();
  }
  private void InitializeComponent()
  {
     this.dateTimePickerDropOff = new System.Windows.Forms.DateTimePicker();
     this.outputLabel = new System.Windows.Forms.Label();
     this.deliveryLabel = new System.Windows.Forms.Label();
     this.dropOffLabel = new System.Windows.Forms.Label();
     this.SuspendLayout();
     // 
     // dateTimePickerDropOff
     // 
     this.dateTimePickerDropOff.Location = new System.Drawing.Point(31, 39);
     this.dateTimePickerDropOff.Name = "dateTimePickerDropOff";
     this.dateTimePickerDropOff.Size = new System.Drawing.Size(200, 20);
     this.dateTimePickerDropOff.TabIndex = 0;
     this.dateTimePickerDropOff.ValueChanged += new System.EventHandler(this.dateTimePickerDropOff_ValueChanged);
     // 
     // outputLabel
     // 
     this.outputLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
     this.outputLabel.Location = new System.Drawing.Point(31, 108);
     this.outputLabel.Name = "outputLabel";
     this.outputLabel.Size = new System.Drawing.Size(200, 23);
     this.outputLabel.TabIndex = 1;
     // 
     // deliveryLabel
     // 
     this.deliveryLabel.AutoSize = true;
     this.deliveryLabel.Location = new System.Drawing.Point(30, 95);
     this.deliveryLabel.Name = "deliveryLabel";
     this.deliveryLabel.Size = new System.Drawing.Size(119, 13);
     this.deliveryLabel.TabIndex = 2;
     // 
     // dropOffLabel
     // 
     this.dropOffLabel.AutoSize = true;
     this.dropOffLabel.Location = new System.Drawing.Point(30, 23);
     this.dropOffLabel.Name = "dropOffLabel";
     this.dropOffLabel.Size = new System.Drawing.Size(72, 13);
     this.dropOffLabel.TabIndex = 3;
     this.dropOffLabel.Text = "Drop Off Date:";
     // 
     // DateTimePickerForm
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
     this.ClientSize = new System.Drawing.Size(262, 235);
     this.Controls.Add(this.dropOffLabel);
     this.Controls.Add(this.deliveryLabel);
     this.Controls.Add(this.outputLabel);
     this.Controls.Add(this.dateTimePickerDropOff);
     this.Name = "DateTimePickerForm";
     this.Text = "DateTimePickerTest";
     this.ResumeLayout(false);
     this.PerformLayout();

   }
  [STAThread]
  static void Main()
 {
   Application.EnableVisualStyles();
   Application.Run(new Form1());
 }

}


      </source>


Set DateTimePicker Min Date and Max Date

<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 class Form1 : Form {

   private System.Windows.Forms.DateTimePicker dateTimePickerDropOff;
   private System.Windows.Forms.Label outputLabel;
   private System.Windows.Forms.Label deliveryLabel;
   private System.Windows.Forms.Label dropOffLabel;
   public Form1() {
       InitializeComponent();
       dateTimePickerDropOff.MinDate = DateTime.Today;
       dateTimePickerDropOff.MaxDate = DateTime.Today.AddYears( 1 );
 }
  private void dateTimePickerDropOff_ValueChanged( object sender, EventArgs e )
  {
     DateTime dropOffDate = dateTimePickerDropOff.Value;
     if ( dropOffDate.DayOfWeek == DayOfWeek.Friday ||
        dropOffDate.DayOfWeek == DayOfWeek.Saturday ||
        dropOffDate.DayOfWeek == DayOfWeek.Sunday )
        outputLabel.Text = dropOffDate.AddDays( 3 ).ToLongDateString();
     else
        outputLabel.Text = dropOffDate.AddDays( 2 ).ToLongDateString();
  }
  private void InitializeComponent()
  {
     this.dateTimePickerDropOff = new System.Windows.Forms.DateTimePicker();
     this.outputLabel = new System.Windows.Forms.Label();
     this.deliveryLabel = new System.Windows.Forms.Label();
     this.dropOffLabel = new System.Windows.Forms.Label();
     this.SuspendLayout();
     // 
     // dateTimePickerDropOff
     // 
     this.dateTimePickerDropOff.Location = new System.Drawing.Point(31, 39);
     this.dateTimePickerDropOff.Name = "dateTimePickerDropOff";
     this.dateTimePickerDropOff.Size = new System.Drawing.Size(200, 20);
     this.dateTimePickerDropOff.TabIndex = 0;
     this.dateTimePickerDropOff.ValueChanged += new System.EventHandler(this.dateTimePickerDropOff_ValueChanged);
     // 
     // outputLabel
     // 
     this.outputLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
     this.outputLabel.Location = new System.Drawing.Point(31, 108);
     this.outputLabel.Name = "outputLabel";
     this.outputLabel.Size = new System.Drawing.Size(200, 23);
     this.outputLabel.TabIndex = 1;
     // 
     // deliveryLabel
     // 
     this.deliveryLabel.AutoSize = true;
     this.deliveryLabel.Location = new System.Drawing.Point(30, 95);
     this.deliveryLabel.Name = "deliveryLabel";
     this.deliveryLabel.Size = new System.Drawing.Size(119, 13);
     this.deliveryLabel.TabIndex = 2;
     // 
     // dropOffLabel
     // 
     this.dropOffLabel.AutoSize = true;
     this.dropOffLabel.Location = new System.Drawing.Point(30, 23);
     this.dropOffLabel.Name = "dropOffLabel";
     this.dropOffLabel.Size = new System.Drawing.Size(72, 13);
     this.dropOffLabel.TabIndex = 3;
     this.dropOffLabel.Text = "Drop Off Date:";
     // 
     // DateTimePickerForm
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
     this.ClientSize = new System.Drawing.Size(262, 235);
     this.Controls.Add(this.dropOffLabel);
     this.Controls.Add(this.deliveryLabel);
     this.Controls.Add(this.outputLabel);
     this.Controls.Add(this.dateTimePickerDropOff);
     this.Name = "DateTimePickerForm";
     this.Text = "DateTimePickerTest";
     this.ResumeLayout(false);
     this.PerformLayout();

   }
  [STAThread]
  static void Main()
 {
   Application.EnableVisualStyles();
   Application.Run(new Form1());
 }

}


      </source>