We help IT Professionals succeed at work.

DateTimePicker in different formats

Molnár István
Molnár István used Ask the Experts™
on
I need a free DateTimePicker control in Winforms or an ideea, to do the following:

- allow nulls
- it allows me to enter just a YEAR
- it allows me to enter Month/YEAR
- it allows me to enter Day/Month/Year

I found a DateTimePicker control on CodeProject that allows nullabale value using the DEL button, but the format is Day/Month/Year.

This control I would like to add later to rows in a Grid.

Thanks advanced, Steve
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Winforms already has a DateTimePicker control that meets your requirements.  You simply need to provide a custom format; e.g. -

Form1.Designer.cs -
namespace EE_Q29166878
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
            this.dateTimePicker2 = 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.dateTimePicker3 = new System.Windows.Forms.DateTimePicker();
            this.SuspendLayout();
            // 
            // dateTimePicker1
            // 
            this.dateTimePicker1.CustomFormat = "yyyy";
            this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.dateTimePicker1.Location = new System.Drawing.Point(15, 25);
            this.dateTimePicker1.Name = "dateTimePicker1";
            this.dateTimePicker1.Size = new System.Drawing.Size(200, 20);
            this.dateTimePicker1.TabIndex = 0;
            // 
            // dateTimePicker2
            // 
            this.dateTimePicker2.CustomFormat = "MM/yyyy";
            this.dateTimePicker2.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.dateTimePicker2.Location = new System.Drawing.Point(15, 79);
            this.dateTimePicker2.Name = "dateTimePicker2";
            this.dateTimePicker2.Size = new System.Drawing.Size(200, 20);
            this.dateTimePicker2.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(29, 13);
            this.label1.TabIndex = 2;
            this.label1.Text = "Year";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(12, 63);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(64, 13);
            this.label2.TabIndex = 3;
            this.label2.Text = "Month/Year";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(12, 114);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(49, 13);
            this.label3.TabIndex = 5;
            this.label3.Text = "Full Date";
            // 
            // dateTimePicker3
            // 
            this.dateTimePicker3.CustomFormat = "dd/MM/yyyy";
            this.dateTimePicker3.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.dateTimePicker3.Location = new System.Drawing.Point(15, 130);
            this.dateTimePicker3.Name = "dateTimePicker3";
            this.dateTimePicker3.Size = new System.Drawing.Size(200, 20);
            this.dateTimePicker3.TabIndex = 4;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(249, 167);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.dateTimePicker3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.dateTimePicker2);
            this.Controls.Add(this.dateTimePicker1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.DateTimePicker dateTimePicker1;
        private System.Windows.Forms.DateTimePicker dateTimePicker2;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.DateTimePicker dateTimePicker3;
    }
}

Open in new window

Form1.cs -
using System.Windows.Forms;

namespace EE_Q29166878
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

Open in new window

Produces the following output -Capture.PNG-saige-
Molnár IstvánHelpDesk / Programmer

Author

Commented:
The problem is, that I want just 1 DateTimePicker, because later on i will need to add it to GridView rows.
Molnár IstvánHelpDesk / Programmer

Author

Commented:
OK, i solved it using MaskedTextBox:

        MaskedTextBox maskedTextBox;
----------------------------------------------------------------------
            this.maskedTextBox = new MaskedTextBox();

            this.maskedTextBox.Visible = false;

            this.dataGridView1.Controls.Add(this.maskedTextBox);

            this.dataGridView1.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);

            this.dataGridView1.CellEndEdit +=  new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
            
            this.dataGridView1.Scroll += new ScrollEventHandler(dataGridView1_Scroll);
----------------------------------------------------------------------
	   void dataGridView1_Scroll(object sender, ScrollEventArgs e)
        {
            if (this.maskedTextBox.Visible)
            {
                //we have to adjust the location for the MaskedTextBox while scrolling
                Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(
                    this.dataGridView1.CurrentCell.ColumnIndex,
                    this.dataGridView1.CurrentCell.RowIndex, true);
                this.maskedTextBox.Location = rect.Location;
            }
        }

        void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (e.ColumnIndex == this.dataGridView1.Columns[4].Index || e.ColumnIndex == this.dataGridView1.Columns[5].Index && e.RowIndex > -1)
            {
                string type = this.dataGridView1[4, e.RowIndex].Value.ToString();
                //if (type == "Home Phone" || type == "Cell" || type == "Work")
                //{
                    this.maskedTextBox.Mask = "0000.00.00";
                    Rectangle rect =
                       this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);

                    this.maskedTextBox.Location = rect.Location;
                    this.maskedTextBox.Size = rect.Size;
                    this.maskedTextBox.Text = "";

                    if (this.dataGridView1[e.ColumnIndex, e.RowIndex].Value != null)
                    {
                        this.maskedTextBox.Text = this.dataGridView1[e.ColumnIndex,
                            e.RowIndex].Value.ToString();
                    }
                    this.maskedTextBox.Visible = true;
                }
                // if type is Email, do no show the MaskedTextBox
          //  }
        }

        void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (this.maskedTextBox.Visible)
            {
                this.dataGridView1.CurrentCell.Value = this.maskedTextBox.Text;
                this.maskedTextBox.Visible = false;
            }
        }

Open in new window

HelpDesk / Programmer
Commented:
The solution I found is using Masked TextBox
Molnár IstvánHelpDesk / Programmer

Author

Commented:
Thanks for the quick answer.