|
While this allowed the control to go back to edit mode after it was set to null, I didn't like the way it would go back to Today's date. The reason being... Today is 09/09/09. Well, lets say I want to go into that field and enter in an April date, I will hit 4 and it will display 09/09/2009. I will have to hit 4 again to enter the April date.
I tried moving the base.OnKeyUp(e) call after the code to set today's date so it would persist the 4 into the control and that didn't work. Then I tried moving the code to the OnKeyDown event handler. That didn't work either. So I found away around it be using some date manipulation:
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
this.Value = null;
}
else if ((this.Value == DateTime.MinValue || this.Value == null)
&& (e.KeyCode == Keys.Space || Char.IsNumber((char)e.KeyValue)
|| e.KeyCode == Keys.Up || e.KeyCode == Keys.Down
|| e.KeyCode == Keys.Right || e.KeyCode == Keys.Left))
{
DateTime newDate = DateTime.Today;
if (Char.IsNumber((char)e.KeyValue))
{
newDate = DateTime.Parse(string.Format("{0}/{1}/{2}", (char)e.KeyValue, DateTime.Today.Day, DateTime.Today.Year));
}
this.Value = newDate;
}
base.OnKeyDown(e);
}
NOTE: I have also included a check for this.Value == null because I also altered the class to use a Nullable(DateTime). Also, this is in the OnKeyDown event. I moved this from the previously suggested OnKeyUp. I think it will work either way, though.
|
|
|
|
|
Hi there,
I have fixed some bugs and...
extended it to...
- use two digits
- international support
- using numpad
bugs fixed:
- using Zero "0" numeric exception
- always got april
- can't use two digits
- null value exception
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Delete)
{
this.Value = DateTime.MinValue;
}
else if ((this.Value == DateTime.MinValue || this.Value == null)
&& (e.KeyCode == Keys.Space || (Char.IsNumber((char)e.KeyValue) && e.KeyValue != 48)
|| e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Right
|| e.KeyCode == Keys.Left
|| (e.KeyValue >= 97 && e.KeyValue <= 105)
))
{
int typedDigit = 1;
if (e.KeyValue >= 97 && e.KeyValue <= 105)
{
typedDigit = int.Parse(((char)e.KeyValue - 46).ToString());
}
else
{
typedDigit = int.Parse(((char)e.KeyValue).ToString());
}
DateTime newDate = DateTime.Today;
if (Char.IsNumber((char)e.KeyValue))
{
newDate = new DateTime(DateTime.Today.Year, DateTime.Today.Month, typedDigit);
}
this.Value = newDate;
SendKeys.Send(typedDigit.ToString());
}
}
modified on Friday, December 4, 2009 9:02 AM
|
|
|
|
|
I got an exception on this line when i pressed space and the value is null :
typedDigit = int.Parse(((char)e.KeyValue).ToString());
I fixed it like this :
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Delete)
{
this.Value = DateTime.MinValue;
}
else if (this.Value == DateTime.MinValue || this.Value == null)
{
if (e.KeyCode == Keys.Space || e.KeyCode == Keys.Up || e.KeyCode == Keys.Down
|| e.KeyCode == Keys.Right || e.KeyCode == Keys.Left)
{
this.Value = DateTime.Today;
}
else if ((Char.IsNumber((char)e.KeyValue) && e.KeyValue != 48)
|| (e.KeyValue >= 97 && e.KeyValue <= 105))
{
int typedDigit = 1;
if (e.KeyValue >= 97 && e.KeyValue <= 105)
{
typedDigit = int.Parse(((char)(e.KeyValue - 48)).ToString());
}
else
{
typedDigit = int.Parse(((char)e.KeyValue).ToString());
}
this.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, typedDigit);
SendKeys.Send(typedDigit.ToString());
}
}
}
modified on Friday, December 11, 2009 1:29 PM
|
|
|
|
|
thanks =)
I have fixed an another issue too...
When you enter a date by keyboard and a MaxDate or MinDate is set, it will throw an exception.
I change the code again...
public new DateTime Value
{
get
{
if (bIsNull)
return DateTime.MinValue;
else
return base.Value;
}
set
{
if (value == DateTime.MinValue)
{
if (bIsNull == false)
{
oldFormat = this.Format;
oldCustomFormat = this.CustomFormat;
bIsNull = true;
}
this.Format = DateTimePickerFormat.Custom;
this.CustomFormat = " ";
}
else
{
if (bIsNull)
{
this.Format = oldFormat;
this.CustomFormat = oldCustomFormat;
bIsNull = false;
}
if (value < this.MaxDate && value > this.MinDate)
{
base.Value = value;
}
else if (value > this.MaxDate)
{
value = this.MaxDate;
}
else if (value < this.MinDate)
{
value = this.MinDate;
}
}
}
}
|
|
|
|
|
I am back *smile*
Found a bug, when changing the DateTime Value to nothing (DateTime.MinValue) the ValueChanged Event will not be raised, so I put in an Eventraiser...
public new DateTime Value
{
get
{
if (bIsNull)
return DateTime.MinValue;
else
return base.Value;
}
set
{
if (value == DateTime.MinValue)
{
if (bIsNull == false)
{
oldFormat = this.Format;
oldCustomFormat = this.CustomFormat;
bIsNull = true;
}
this.Format = DateTimePickerFormat.Custom;
this.CustomFormat = " ";
base.OnValueChanged(new EventArgs());
}
else
{
if (bIsNull)
{
this.Format = oldFormat;
this.CustomFormat = oldCustomFormat;
bIsNull = false;
}
if (value < this.MaxDate && value > this.MinDate)
{
base.Value = value;
}
else if (value > this.MaxDate)
{
value = this.MaxDate;
}
else if (value < this.MinDate)
{
value = this.MinDate;
}
}
}
}
|
|
|
|
|
|
I found this pretty usefull, but do you know a way to place a button in the calendar to place the null value? like the "today" button there is in the lower left corner... I'm using vb.net :P just so you know.
thanks!
|
|
|
|
|
The sample project doesn't work for me -- pressing delete does nothing. What gives?
How do I use this control in a project?
Relative performance is no substitute for absolute achievement.
|
|
|
|
|
I like it
Actually change Value so it is a "DateTime?", much nicer
And also I used both Delete & Backspace for having an empty date
|
|
|
|
|
When NullableDateTimePicker (NDTP) state is null and it doesn't show value, click on the dropdown and select current value at the bottom of the dropdown, near red circle mark. This should change the NDTP state to not-null and show current DateTime. But NDTP checks if user selected value in dropdown by checking that no mouse button is pressed (in OnCloseUp), which is true only if a calendar was clicked, but not if bottom mark was clicked.
IMO, there is solution for this is not to rely on mouse button state in OnCloseUp. Instead override OnValueChanged and set a flag, then use it in OnCloseUp.
Thus:
<br />
protected override void OnCloseUp(EventArgs eventargs) {<br />
if (Control.MouseButtons == MouseButtons.None || fHasValueChanged) {<br />
if (fIsNull) {<br />
this.Format = fOldFormat;<br />
this.CustomFormat = fOldCustomFormat;<br />
fIsNull = false;<br />
}<br />
fHasValueChanged = false;<br />
}<br />
base.OnCloseUp(eventargs);<br />
}<br />
protected override void OnKeyUp(KeyEventArgs e) {<br />
base.OnKeyUp(e);<br />
if (e.KeyCode == Keys.Delete) {<br />
this.Value = DateTime.MinValue;<br />
this.fHasValueChanged = false;<br />
}<br />
}<br />
protected override void OnValueChanged(EventArgs eventargs) {<br />
fHasValueChanged = true;<br />
base.OnValueChanged(eventargs);<br />
}<br />
Resetting to false in OnKeyUp has to be done, because user can change value by hand, not by dropdown. In such case the flag remains true which could cause problems if user presses delete on NDTP and then pressed and depressed dropdown arrow (without changing value). If flag is not reset when she pressed delete, than suddenly NDTP would show value.
|
|
|
|
|
hi all,
i am using sql server and vb.net 2002 to develop my project.
the problem i face now is when i bind the null value (datatype = datetime) into the date time picker control, it show an error
please help, i need the code in vb.net
loon
Have a nice day
|
|
|
|
|
ccloon wrote:
i am using sql server and vb.net 2002 to develop my project.
the problem i face now is when i bind the null value (datatype = datetime) into the date time picker control, it show an error
Have a look at this version of a nullable date time picker.
http://www.omnitalented.com/Blog/PermaLink,guid,9ee757fe-a3e8-46f7-ad04-ef7070934dc8.aspx
It can handle "real" null values as you get them from databases.
You request it in VB. I'm sorry, it's in C#. But it's simple. I'm sure you manage to translate it.
Claudio Grazioli
http://www.grazioli.ch
|
|
|
|
|
|
I tried this version and binding a nullable datetime does not work, as it leaves the last valid date from a previous bind. Here is a different version that seems to work better. NB. 5 minutes of testing only. Just dataBind to the ValueNullable property as opposed to the Value property if you have a Nullable DateTime. This should work as normal if you have a normal DateTime (bind to Value).
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace Invisage.PinkFloyd.Library.Controls
{
public class DateTimePickerNullable : System.Windows.Forms.DateTimePicker
{
DateTimePickerFormat mOriginaldateTimePickerFormat;
private DateTime? mValueNullable;
public DateTimePickerNullable()
: base()
{
mOriginaldateTimePickerFormat = base.Format;
}
[Category("PinkFloyd")]
[Description("Nullable DateTime")]
[Bindable(true)]
public DateTime? ValueNullable
{
get { return mValueNullable; }
set
{
mValueNullable = value;
if (!mValueNullable.HasValue)
{
this.Format = DateTimePickerFormat.Custom;
this.CustomFormat = " ";
}
else
{
this.Format = mOriginaldateTimePickerFormat;
base.Value = mValueNullable.Value;
}
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown (e);
if (e.KeyCode == Keys.Delete)
{
ValueNullable = null;
}
}
}
}
|
|
|
|
|
Have you tried pressing the Delete button when the ValueNullable it is bound? I think you'll find it doesn't work.
|
|
|
|
|
Your control could possibly be what I am looking for.
I want to be able to display either a blank space or a customer phrase (such as "No Date Entered" when the date is NULL.
Can your control handle this?
|
|
|
|
|
|
Thanks!
Just what I was looking for!
Thank you so much!
Janine
|
|
|
|
|
Hi Pham,
First off, excellent control. I have been looking far and wide for something like this, and I thought that I had found my solution in your control, but I am having difficulty placing it in a TabPage on a TabControl control AND data binding.
For example, if I start off with your DateTimePicker control in a TabPage container that is not by default displayed, no problem--I can choose the TabPage and it displays your control fine. Your control operates fine, and I can even set it to "null" (that is, DateTime.MinValue) before selecting the TabPage.
Now add databinding. If I have an additional private DateTime variable and in the form's Load event I add a custom databinding, e.g.
<br />
private DateTime dt = DateTime.MinValue;<br />
<br />
private void Form1_Load(object sender, System.EventArgs e)<br />
{<br />
this.dateTimePicker1.DataBindings.Add("Value", dt, "");<br />
}<br />
then, when I display the TabPage containing the control, I receive:
An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in system.windows.forms.dll
Additional information: Error creating window handle.
Do you know any way to prevent this? Debugging seems to reveal that the error occurs somewhere in the Set clause, but I can't pinpoint it--very strange error.
Thanks!
Mark
|
|
|
|
|
Yep - this one got me too!
It is the change in DateTimePicker.Format when you are switching between a null date and a non-null date (or vice versa). The change in the value of Format causes the component to raise a notification which is normally ok. But in the case you described above the control is not visible yet and the Format notification fails internally.
The work around is to leave the Format as Custom and switch the CustomFormat format between " " for a null date value, and the appropriate format for the date eg for short - "dd/MM/yyyy".
Cheers,
Brett
|
|
|
|
|
|
Appreciate your handiwork!
|
|
|
|
|
Hi there, I'd like to say that is very good idea, but could you convert it to vb.net version with the add function, may it help all people who need this one.
|
|
|
|
|
Did you try the case where u start with a null datetime ?
If you do that, you can't select anything anymore!
cheers !
|
|
|
|
|
Surely anyone who can't convert C# to VB.NET should consider a career as a plumber ?
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|