This is what I use.
namespace ControlLibrary { public partial class DatePicker : BaseControl { public String _Heading { get { return label1.Text; } set { label1.Text = (value ?? string.Empty).ToString(); } } public String _Text { get; set; } public Boolean _Queued { get; set; } public Gizmox.WebGUI.Forms.DateTimePicker dateTimePicker1 { get; set; }
public DatePicker() { InitializeComponent(); _Queued = true; }
void splitContainer1_Resize(object sender, EventArgs e) { splitContainer1.SplitterDistance = (splitContainer1.Size.Width / 2) - 1; splitContainer1.Panel1.Update(); splitContainer1.Panel2.Update(); } public override BaseControl Initialize(BaseControl Parent) { base.BaseInitialize(Parent, this); splitContainer1.SplitterDistance = (splitContainer1.Size.Width / 2) - 1; splitContainer1.Panel1.Update(); splitContainer1.Panel2.Update(); splitContainer1.Resize += new EventHandler(splitContainer1_Resize); if (_ColumnValue == null) _ColumnValue = Null.NullDate; if ((DateTime)_ColumnValue != null && (DateTime)_ColumnValue > Null.NullDate) { this.splitContainer1.Panel2.Controls.Clear(); dateTimePicker1 = new DateTimePicker(); dateTimePicker1.Size = textBox1.Size; dateTimePicker1.Dock = DockStyle.Fill; dateTimePicker1.Value = (DateTime)_ColumnValue; dateTimePicker1.Format = DateTimePickerFormat.Short; if (_Queued) dateTimePicker1.ValueChangedQueued += new EventHandler(dp_ValueChangedQueued); else dateTimePicker1.ValueChanged += new EventHandler(dp_ValueChanged); _Text = dateTimePicker1.Value.ToShortDateString(); splitContainer1.Panel2.Controls.Add(dateTimePicker1); } else { textBox1.Click += new EventHandler(textBox1_Click); textBox1.TextChanged +=new EventHandler(textBox1_TextChanged); } return this; }
void textBox1_TextChanged(object sender, EventArgs e) { if (dateTimePicker1 == null) { _ColumnValue = DateTime.Now; this.splitContainer1.Panel2.Controls.Clear(); dateTimePicker1 = new DateTimePicker(); dateTimePicker1.Size = textBox1.Size; dateTimePicker1.Dock = DockStyle.Fill; dateTimePicker1.Value = (DateTime)_ColumnValue; dateTimePicker1.Format = DateTimePickerFormat.Short; if (_Queued) dateTimePicker1.ValueChangedQueued += new EventHandler(dp_ValueChangedQueued); else dateTimePicker1.ValueChanged += new EventHandler(dp_ValueChanged); splitContainer1.Panel2.Controls.Add(dateTimePicker1); _Text = dateTimePicker1.Value.ToShortDateString(); Text = DateTime.Now.ToLongTimeString(); } }
void textBox1_Click(object sender, EventArgs e) { if (dateTimePicker1 == null) { _ColumnValue = DateTime.Now; this.splitContainer1.Panel2.Controls.Clear(); dateTimePicker1 = new DateTimePicker(); dateTimePicker1.Size = textBox1.Size; dateTimePicker1.Dock = DockStyle.Fill; dateTimePicker1.Value = (DateTime)_ColumnValue; dateTimePicker1.Format = DateTimePickerFormat.Short; if (_Queued) dateTimePicker1.ValueChangedQueued += new EventHandler(dp_ValueChangedQueued); else dateTimePicker1.ValueChanged += new EventHandler(dp_ValueChanged); splitContainer1.Panel2.Controls.Add(dateTimePicker1); _Text = dateTimePicker1.Value.ToShortDateString(); Text = DateTime.Now.ToLongTimeString(); } }
void dp_ValueChanged(object sender, EventArgs e) { try { _ColumnValue = dateTimePicker1.Value; _Text = dateTimePicker1.Value.ToShortDateString(); Text = DateTime.Now.ToLongTimeString(); } catch { } }
void dp_ValueChangedQueued(object sender, EventArgs e) { try { _ColumnValue = dateTimePicker1.Value; _Text = dateTimePicker1.Value.ToShortDateString(); Text = DateTime.Now.ToLongTimeString(); } catch { } } } }
You'll have to strip out the basecontrol stuff, but if the _ColumnValue is null an empty textbox is displayed, as soon as the user clicks in the textbox it is replaced with a datetime picker set to the current date.
Hope this helps,
Ron |
|