|
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
|
|
|
|
|
Here is a VB.Net version. In addition to allowing the date to be deleted (via F2 or Delete keys), the control's background color changes when it receives focus.
Public Class DateTimeControl
Inherits System.Windows.Forms.DateTimePicker
Private mdtmValue As Date = Now()
Private mstrCustomFormat As String
Private mdtpFormat As DateTimePickerFormat = DateTimePickerFormat.Long
Private mblnSettoNull As Boolean
Private mdtmMinValue As Date = DateTime.MinValue
Private mstrText As String
Private mBackBrush As SolidBrush
<Description("Accepts legitimate date value or DateTime.MinValue."), MergableProperty(False), Category("Behavior")> _
Public Shadows Property Value() As Date
'This property definition overrides the Value property for the DateTimePicker control
Get
'Return value stored in hidden variable
Return mdtmValue
End Get
Set(ByVal Value As Date)
'Set the hidden variable mdtValue
If mblnSettoNull = True Or Value = DateTime.MinValue Then
mblnSettoNull = False
mdtmValue = DateTime.MinValue
Else
mdtmValue = Value
MyBase.Value = mdtmValue
End If
End Set
End Property
<Description("Accepts a legitimate date or time format string."), MergableProperty(False), Category("Behavior")> _
Public Shadows Property CustomFormat() As String
Get
Return mstrCustomFormat
End Get
Set(ByVal Value As String)
mstrCustomFormat = Value
MyBase.CustomFormat = mstrCustomFormat
End Set
End Property
<DefaultValue(1), Description("Accepts a legitimate format such as Long, Short, Time or Custom."), MergableProperty(False), Category("Behavior")> _
Public Shadows Property Format() As DateTimePickerFormat
Get
Return mdtpFormat
End Get
Set(ByVal Value As DateTimePickerFormat)
mdtpFormat = Value
MyBase.Format = mdtpFormat
End Set
End Property
Public Shadows Property BackColor() As Color
Get
Return MyBase.BackColor
End Get
Set(ByVal Value As Color)
If Not mBackBrush Is Nothing Then
mBackBrush.Dispose()
End If
MyBase.BackColor = Value
mBackBrush = New SolidBrush(Me.BackColor)
Me.Invalidate()
End Set
End Property
Protected Overrides Sub WndProc(ByRef m As Message)
Const WM_ERASEBKGND As Int32 = &H14
If m.Msg = WM_ERASEBKGND Then
Dim g As Graphics = Graphics.FromHdc(m.WParam)
If mBackBrush Is Nothing Then
mBackBrush = New SolidBrush(Me.BackColor)
End If
g.FillRectangle(mBackBrush, Me.ClientRectangle)
g.Dispose()
Else
MyBase.WndProc(m)
End If
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
'Clear out when Delete of F2 is pressed
If e.KeyCode = Keys.Delete OrElse e.KeyCode = Keys.F2 Then
mblnSettoNull = True 'Flag to indicate null value desired
Me.Format = DateTimePickerFormat.Custom
Me.CustomFormat = " "
Me.Value = DateTime.MinValue
ElseIf e.KeyCode = Keys.Down OrElse e.KeyCode = Keys.Left OrElse e.KeyCode = Keys.Right OrElse e.KeyCode = Keys.Up Then
If IsNothing(Me.Tag) Then
Me.Format = DateTimePickerFormat.Custom
Me.CustomFormat = "MM/dd/yyyy"
Else
Me.Format = DateTimePickerFormat.Custom
Me.CustomFormat = "MM/dd/yyyy hh:mm tt"
End If
End If
End Sub
Protected Overrides Sub OnCloseUp(ByVal eventargs As System.EventArgs)
'This event fires when the user dismisses the graphical calendar
Me.Format = DateTimePickerFormat.Custom
If IsNothing(Me.Tag) Then
Me.CustomFormat = "MM/dd/yyyy"
Else
Me.CustomFormat = "MM/dd/yyyy hh:mm tt"
End If
Me.Value = MyBase.Value
End Sub
Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
Me.BackColor = System.Drawing.SystemColors.Window
End Sub
Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
Me.BackColor = System.Drawing.Color.LightSteelBlue
End Sub
End Class
|
|
|
|
|
Hi, anyone knows how do I overrides the value property in the class? (im trying to translate the c# code)
I get a message saying that the property in the base class has not been declared as overridable so this won't work
vb.net
Public Overrides Property Value() As DateTime
Get
End Get
Set(ByVal Value As DateTime)
End Set
End Property
Could you please reply to
pietrud@hotmail.com
Thanks a bunch!
|
|
|
|
|
It's not an override : you have to mask the original property (in C# it's done with the new keyword; in VB.NET, it is Shadows;
For instance, this piece of code does nothing, but it works :
Public Class MyDateTimePicker
Inherits DateTimePicker
Public Shadows Property Value() As DateTime
Get
Return MyBase.Value
End Get
Set(ByVal Value As DateTime)
Value = MyBase.Value
End Set
End Property
End Class
|
|
|
|
|
You can add support to the ShowUpDown property usually used for Time Format:
protected override void OnMouseUp(MouseEventArgs e)
{
if(this.ShowUpDown)
{
if (bIsNull)
{
this.Format = oldFormat;
this.CustomFormat = oldCustomFormat;
this.Value = DateTime.Now;
bIsNull = false;
}
}
base.OnMouseUp(e);
}
|
|
|
|
|
Your solution is simple and elegant, but two corrections should be made I think :
1- use OnKeyUp instead of OnKeyDown , because the latter is fired up repeatedly until the user releases the key.
2- You should fire up OnValueChanged manually when setting value to DateTime.MinValue . Here is the modified code :
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;
}
base.Value = value;
}
}
}
Sébastien
Intelligence shared is intelligence squared.
Homepage : http://www.slorion.webhop.org
|
|
|
|
|
How can I set the cursor in the DateTimePicker control, which indicates focus on this control to user, after setting its value to null, and tabbing to it from other controls?
|
|
|
|
|
Hi all! I want to select this control with tab, and I found a solution, but I think it not the best. If somebody have an idea, share it with me please.
Here is the solution:
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 = this.Format.toString();
this.CustomFormat = "yyyy";
base.OnValueChanged(new EventArgs());
}
else
{
if (bIsNull)
{
this.Format = oldFormat;
this.CustomFormat = oldCustomFormat;
bIsNull = false;
}
base.Value = value;
}
}
}
|
|
|
|
|
Hi,
I want to use this code in vb. I created a class and converted from C to VB. I am not sure how to use the control. When I reference the namespace.datetimepicker inside my form, it blows up trying to wrtie the control. I do not see the control on my toolbox. I am clueless here how to implement this.
Thanks.
|
|
|
|
|
I'm sorry I don't do this override stuff. How do I implement this code? Do I have to create a class? Can I add code to the existing datetimepicker? What do I do?
|
|
|
|
|
Just drag standard DateTimePicker control onto the form, then go to Code view and change its type to our extension class.
Cheers.
|
|
|
|