Csharp/CSharp Tutorial/GUI Windows Forms/DateTimePicker

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

DateTimePicker custom format

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.DateTimePicker dateTimePicker1;
    private System.Windows.Forms.DateTimePicker dateTimePicker2;
    private System.Windows.Forms.DateTimePicker dateTimePicker3;
    private System.Windows.Forms.DateTimePicker dateTimePicker4;
    private System.Windows.Forms.DateTimePicker dateTimePicker5;
    private System.Windows.Forms.DateTimePicker dateTimePicker6;
    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.Label label5;
    private System.Windows.Forms.Label label7;
    public Form1() {
        this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
        this.dateTimePicker2 = new System.Windows.Forms.DateTimePicker();
        this.dateTimePicker3 = new System.Windows.Forms.DateTimePicker();
        this.dateTimePicker4 = new System.Windows.Forms.DateTimePicker();
        this.dateTimePicker5 = new System.Windows.Forms.DateTimePicker();
        this.dateTimePicker6 = 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.label5 = new System.Windows.Forms.Label();
        this.label7 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // dateTimePicker1
        // 
        this.dateTimePicker1.CustomFormat = "hh:mm:s : MM/dd/yy";
        this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
        this.dateTimePicker1.Location = new System.Drawing.Point(16, 16);
        this.dateTimePicker1.Name = "dateTimePicker1";
        this.dateTimePicker1.Size = new System.Drawing.Size(240, 20);
        this.dateTimePicker1.TabIndex = 0;
        // 
        // dateTimePicker2
        // 
        this.dateTimePicker2.CustomFormat = "h/mm/ss : dd.MM.yyyy";
        this.dateTimePicker2.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
        this.dateTimePicker2.Location = new System.Drawing.Point(16, 56);
        this.dateTimePicker2.Name = "dateTimePicker2";
        this.dateTimePicker2.Size = new System.Drawing.Size(240, 20);
        this.dateTimePicker2.TabIndex = 0;
        // 
        // dateTimePicker3
        // 
        this.dateTimePicker3.CustomFormat = "HH-mm-ss : MMMM/dd/yyyy  tt";
        this.dateTimePicker3.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
        this.dateTimePicker3.Location = new System.Drawing.Point(16, 88);
        this.dateTimePicker3.Name = "dateTimePicker3";
        this.dateTimePicker3.Size = new System.Drawing.Size(240, 20);
        this.dateTimePicker3.TabIndex = 0;
        // 
        // dateTimePicker4
        // 
        this.dateTimePicker4.CustomFormat = "tt hh/mm/ss : dddd.MMMM.yyyy";
        this.dateTimePicker4.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
        this.dateTimePicker4.Location = new System.Drawing.Point(16, 200);
        this.dateTimePicker4.Name = "dateTimePicker4";
        this.dateTimePicker4.Size = new System.Drawing.Size(240, 20);
        this.dateTimePicker4.TabIndex = 0;
        // 
        // dateTimePicker5
        // 
        this.dateTimePicker5.CustomFormat = "HH/mm/ss : ddd-MM-yy";
        this.dateTimePicker5.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
        this.dateTimePicker5.Location = new System.Drawing.Point(16, 160);
        this.dateTimePicker5.Name = "dateTimePicker5";
        this.dateTimePicker5.Size = new System.Drawing.Size(240, 20);
        this.dateTimePicker5.TabIndex = 0;
        // 
        // dateTimePicker6
        // 
        this.dateTimePicker6.CustomFormat = "hh/mm/ss : dddd MMMM yyyy tt";
        this.dateTimePicker6.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
        this.dateTimePicker6.Location = new System.Drawing.Point(16, 128);
        this.dateTimePicker6.Name = "dateTimePicker6";
        this.dateTimePicker6.Size = new System.Drawing.Size(240, 20);
        this.dateTimePicker6.TabIndex = 0;
        this.dateTimePicker6.Value = new System.DateTime(2002, 5, 14, 14, 28, 0, 0);
        // 
        // label1
        // 
        this.label1.Font = new System.Drawing.Font("Courier New", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
        this.label1.Location = new System.Drawing.Point(264, 16);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(352, 23);
        this.label1.TabIndex = 1;
        this.label1.Text = "Format : hh:mm:ss : MM/dd/yy";
        // 
        // label2
        // 
        this.label2.Font = new System.Drawing.Font("Courier New", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
        this.label2.Location = new System.Drawing.Point(264, 56);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(352, 23);
        this.label2.TabIndex = 2;
        this.label2.Text = "Format : h/mm/ss : dd.MM.yyyy";
        // 
        // label3
        // 
        this.label3.Font = new System.Drawing.Font("Courier New", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
        this.label3.Location = new System.Drawing.Point(264, 88);
        this.label3.Name = "label3";
        this.label3.Size = new System.Drawing.Size(352, 23);
        this.label3.TabIndex = 3;
        this.label3.Text = "Format : HH-mm-ss : MMMM/dd/yyyy  tt";
        // 
        // label4
        // 
        this.label4.Font = new System.Drawing.Font("Courier New", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
        this.label4.Location = new System.Drawing.Point(264, 160);
        this.label4.Name = "label4";
        this.label4.RightToLeft = System.Windows.Forms.RightToLeft.No;
        this.label4.Size = new System.Drawing.Size(352, 23);
        this.label4.TabIndex = 5;
        this.label4.Text = "Format : HH/mm/ss : ddd-MM-yy";
        // 
        // label5
        // 
        this.label5.Font = new System.Drawing.Font("Courier New", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
        this.label5.Location = new System.Drawing.Point(264, 128);
        this.label5.Name = "label5";
        this.label5.Size = new System.Drawing.Size(352, 23);
        this.label5.TabIndex = 4;
        this.label5.Text = "Format : hh/mm/ss : dddd MMMM yyyy tt";
        // 
        // label7
        // 
        this.label7.Font = new System.Drawing.Font("Courier New", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
        this.label7.Location = new System.Drawing.Point(264, 200);
        this.label7.Name = "label7";
        this.label7.Size = new System.Drawing.Size(352, 23);
        this.label7.TabIndex = 6;
        this.label7.Text = "Format : tt hh/mm/ss : dddd.MMMM.yyyy";
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(632, 245);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.label7,
                                      this.label4,
                                      this.label5,
                                      this.label3,
                                      this.label2,
                                      this.label1,
                                      this.dateTimePicker1,
                                      this.dateTimePicker2,
                                      this.dateTimePicker3,
                                      this.dateTimePicker4,
                                      this.dateTimePicker5,
                                      this.dateTimePicker6});
        this.Name = "Form1";
        this.Text = "DateTime Formats";
        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) {
    }
}

DateTimePicker Event and selected value

using System;
using System.Drawing;
using System.Windows.Forms;
public class Calendar : Form
{
  MonthCalendar mc;
  DateTimePicker dtpStart;    
  DateTimePicker dtpEnd;    
  DateTimePicker dtpBold;    
  Label lblStart;
  Label lblEnd;
  Label lblStartDay;
  Label lblBold;
  ComboBox cmbStart;
  Button btnBoldDay;
  Button btnBoldMonthly;
  Button btnBoldAnnually;
  public Calendar()
  {
    Size = new Size(650,450);
    this.Load += new EventHandler(this_Load);
    mc = new MonthCalendar();
    mc.Parent = this;
    mc.Location = new Point(20,20);
    mc.Font = new Font("Times New Roman", 14);
    mc.CalendarDimensions = new Size(2,1);
    mc.FirstDayOfWeek = Day.Monday;
    mc.MaxSelectionCount = 45;
    mc.DateChanged += new DateRangeEventHandler(mc_DateChanged);
    mc.DateSelected += new DateRangeEventHandler(mc_DateSelected);
    lblStart = new Label();
    lblStart.Parent = this;
    lblStart.Text = "Start Date:";
    
    dtpStart = new DateTimePicker();
    dtpStart.Parent = this;
    dtpStart.Size = new Size((int)(Font.Height * .6) * 
              dtpStart.Value.ToString("D").Length,
              dtpStart.PreferredHeight);
    dtpStart.Format = DateTimePickerFormat.Long;
    dtpStart.ShowUpDown = true;
    dtpStart.ValueChanged += new EventHandler(dtpStart_ValueChanged);
    lblEnd = new Label();
    lblEnd.Parent = this;
    lblEnd.Text = "End Date:";
    
    dtpEnd = new DateTimePicker();
    dtpEnd.Parent = this;
    dtpEnd.Size = new Size((int)(Font.Height * .6) * 
              dtpEnd.Value.ToString("D").Length,
              dtpEnd.PreferredHeight);
    dtpEnd.Format = DateTimePickerFormat.Long;
    dtpEnd.ShowUpDown = true;
    dtpEnd.ValueChanged += new EventHandler(dtpEnd_ValueChanged);
        
    lblStartDay = new Label();
    lblStartDay.Parent = this;
    lblStartDay.Text = "Start Day:";
    
    cmbStart = new ComboBox();
    cmbStart.Parent = this;
    cmbStart.DropDownStyle = ComboBoxStyle.DropDownList;
    cmbStart.Items.AddRange(new object[] {"Monday", 
                      "Tuesday", 
                      "Wednesday", 
                      "Thursday", 
                      "Friday", 
                      "Saturday", 
                      "Sunday"});
    cmbStart.SelectedIndex = 0;
    cmbStart.SelectedIndexChanged += new EventHandler(cmbStart_SelectedIndexChanged);
    
    lblBold = new Label();
    lblBold.Parent = this;
    lblBold.Text = "Bold Day:";
    
    dtpBold = new DateTimePicker();
    dtpBold.Parent = this;
    dtpBold.Size = new Size((int)(Font.Height ) * 
              dtpBold.Value.ToString("d").Length,
              dtpBold.PreferredHeight);
    dtpBold.Format = DateTimePickerFormat.Short;
    dtpBold.ShowUpDown = true;
    
    btnBoldDay = new Button();
    btnBoldDay.Parent = this;
    btnBoldDay.Text = "Add Bold Day";
    btnBoldDay.Size = new Size((int)(Font.Height * .6) * 
                btnBoldDay.Text.Length,
                (int)(Font.Height * 1.75));
    btnBoldDay.Click += new EventHandler(btnBoldDay_Click);
    
    btnBoldMonthly = new Button();
    btnBoldMonthly.Parent = this;
    btnBoldMonthly.Text = "Add Bold Day Monthly";
    btnBoldMonthly.Size = new Size((int)(Font.Height * .6) * 
                  btnBoldMonthly.Text.Length,
                  (int)(Font.Height * 1.75));
    btnBoldMonthly.Click += new EventHandler(btnBoldMonthly_Click);
    btnBoldAnnually = new Button();
    btnBoldAnnually.Parent = this;
    btnBoldAnnually.Text = "Add Bold Day Annually";
    btnBoldAnnually.Size = new Size((int)(Font.Height * .6) * 
                    btnBoldAnnually.Text.Length,
                    (int)(Font.Height * 1.75));
    btnBoldAnnually.Click += new EventHandler(btnBoldAnnually_Click);
  }
  static void Main() 
  {
    Application.Run(new Calendar());
  }
  private void this_Load(object sender, EventArgs e)
  {
    lblStart.Location = new Point(mc.Left, mc.Bottom + 10);
    dtpStart.Location = new Point(lblStart.Right, mc.Bottom + 10);
    lblEnd.Location = new Point(mc.Left, lblStart.Bottom + 5);
    dtpEnd.Location = new Point(lblStart.Right, lblStart.Bottom + 5);
    
    lblStartDay.Location = new Point(mc.Left, lblEnd.Bottom + 5);
    cmbStart.Location = new Point(lblStart.Right, lblEnd.Bottom + 5);
    lblBold.Location = new Point(mc.Left, lblStartDay.Bottom + 5);
    dtpBold.Location = new Point(lblBold.Right, 
                  lblStartDay.Bottom + 5);
    
    btnBoldDay.Location = new Point(dtpBold.Right + 10, dtpBold.Top);
    btnBoldMonthly.Location = new Point(btnBoldDay.Right, 
                      dtpBold.Top);
    btnBoldAnnually.Location = new Point(btnBoldMonthly.Right, 
                      dtpBold.Top);
  }
  private void dtpStart_ValueChanged(object sender, EventArgs e)
  {
    mc.SelectionStart = dtpStart.Value;
  }    
  private void dtpEnd_ValueChanged(object sender, EventArgs e)
  {
    mc.SelectionEnd = dtpEnd.Value;
  }    
  private void mc_DateChanged(object sender, DateRangeEventArgs e)
  {
    MessageBox.Show("DateChanged");
    dtpStart.Value = e.Start;
    dtpEnd.Value = e.End;
  }
  private void mc_DateSelected(object sender, DateRangeEventArgs e)
  {
    MessageBox.Show("DateSelected");
  }
  private void cmbStart_SelectedIndexChanged(object sender, 
                        EventArgs e)
  {
    mc.FirstDayOfWeek = (Day)cmbStart.SelectedIndex;
  }
  private void btnBoldDay_Click(object sender, EventArgs e)
  {
    mc.AddBoldedDate(dtpBold.Value);
    mc.UpdateBoldedDates();
  }
  private void btnBoldMonthly_Click(object sender, EventArgs e)
  {
    mc.AddMonthlyBoldedDate(dtpBold.Value);
    mc.UpdateBoldedDates();
  }
  private void btnBoldAnnually_Click(object sender, EventArgs e)
  {
    mc.AddAnnuallyBoldedDate(dtpBold.Value);
    mc.UpdateBoldedDates();
  }
}

DateTimePicker: look and feel and data format

using System;
using System.Drawing;
using System.Windows.Forms;
public class DTPicker : Form
{
  DateTimePicker dtp;    
  public DTPicker()
  {
    Size = new Size(400,300);
    this.Load += new EventHandler(this_Load);
    dtp = new DateTimePicker();
    dtp.Parent = this;
    dtp.Location = new Point(20,20);
    dtp.Size = new Size(ClientSize.Width - 40, dtp.PreferredHeight);
    dtp.Anchor = AnchorStyles.Top | AnchorStyles.Left | 
          AnchorStyles.Right;
    Font fnt = new Font("Times New Roman", 16);
    dtp.CalendarFont = new Font(fnt, 
                  FontStyle.Bold | FontStyle.Italic);
    dtp.CalendarForeColor = Color.Red;
    dtp.CalendarMonthBackground = Color.Yellow;
    dtp.CalendarTitleBackColor = Color.Lime;
    dtp.CalendarTitleForeColor = Color.Blue;
    dtp.CalendarTrailingForeColor = Color.FromArgb(255,192,192);
    dtp.CustomFormat = "dddd,MMMM d, yyyy "at" h:mm:ss tt";
    dtp.Format = DateTimePickerFormat.Custom;
    dtp.DropDownAlign = LeftRightAlignment.Right;
    dtp.ShowUpDown = false;    // default
    dtp.ValueChanged += 
        new EventHandler(dtp_ValueChanged);
  }
  static void Main() 
  {
    Application.Run(new DTPicker());
  }
  private void UpdateLabels()
  {
    Console.WriteLine( dtp.Value.ToString() );
    Console.WriteLine( dtp.Value.ToLongDateString() );
    Console.WriteLine( dtp.Value.ToLongTimeString() );
    Console.WriteLine( dtp.Value.ToShortDateString() );
    Console.WriteLine( dtp.Value.ToShortTimeString() );
  }
  private void this_Load(object sender, EventArgs e)
  {
    UpdateLabels();
  }
  private void dtp_ValueChanged(object sender, EventArgs e)
  {
    UpdateLabels();
  }    
  private void txtCustomString_TextChanged(object sender, EventArgs e)
  {
    UpdateLabels();
  }    
}