Tìm kiếm Blog này

Thứ Năm, 15 tháng 7, 2010

DateTimePicker with null value

The Reason:

If you write a application by VB.NET or C# you known dotnet provide for us a good control to select the DateTime and with it we never care about the validation of value of Date time which is inputted by user. This is DateTimePicker

But there's only one case we can't use this control that is the case we need a control can show null a default value is null. Who ever used the DateTimePicker control in dotNet have to known that we can't set null value for it.

Because of the default value of DateTimePicker is the current Date Time and you cannot set null value for it. So you must use another way to resolve this problem maybe with other control.

But I really like the DateTimePicker Control and I want have all of its properties and events. So I made a decision that is override the DateTimePicker Control of DotNet.

Works have to do

- Override the face of this control.

- Override the “Value”, “Text” properties.

- Override a usual event is “ValueChanged”.

- New properties extra.

The way to resolve:

I use VS2008 with C# to write a new override control

The first, I created a solution and named it is”DataTimePickerExtra” and then add a new ClassLibrary project with the same name.

The second, I remove the default file Classe1.as and then add a Component with the name is “DataTimePickerExtra” too.

The third, I go into the code file “DataTimePickerExtra.cs” an

d change the line

public partial class DateTimePickerExtra : Component

to

public partial class DateTimePickerExtra : DateTimePicker

and all look like that:

namespace DateTimePickerExtra

{

public partial class DateTimePickerExtra : DateTimePicker

{

public DateTimePickerExtra()

{

InitializeComponent();

}

public DateTimePickerExtra(IContainer container)

{

container.Add(this);

InitializeComponent();

}

}

}

The line partial class DateTimePickerExtra : DateTimePicker mean that control is inherited the DateTimePicker Control of DotNet.

The Forth, I go to resolve each work above.

*Override the face of this control.

The result we need in here is have a DateTimePicker control can show the null value. That mean we can delete the value show on control to null and also we can type the date value in it.

My way to do this is insert a TextBox control into the DateTimePicker control by declare a TextBox control like that.

protected TextBox p_txtDateTime;

p_txtDateTime.ReadOnly = true;

p_txtDateTime.BackColor = Color.White;

//add the event for cath the key down and lost focus

p_txtDateTime.KeyDown += new KeyEventHandler(p_txtDateTime_KeyDown);

p_txtDateTime.LostFocus += new EventHandler(p_txtDateTime_LostFocus);

//set the width of text box only cover the text on the DataTimePicker control

p_txtDateTime.Width = this.Width - 34;

p_txtDateTime.Top = -1;

this.Controls.Add(p_txtDateTime);

*Override the “Value”, “Text” properties.

And then we need override two important properties

public new DateTime? Value

{

get

{

if (p_NullableValue)

{

if (string.IsNullOrEmpty(p_txtDateTime.Text))

return null;

}

return base.Value;

}

set

{

if (value != null)

base.Value = (DateTime)value;

else

p_txtDateTime.Text = string.Empty;

}

}

public override string Text

{

get

{

if (p_NullableValue)

{

if (onchangeValue)

{

onchangeValue = false;

return base.Text;

}

return p_txtDateTime.Text;

}

return base.Text;

}

set

{

base.Text = value;

p_txtDateTime.Text = value;

}

}

*Override a usual event is “ValueChanged”.

protected override void OnValueChanged(EventArgs eventargs)

{

if (p_NullableValue)

{

if (!txtonchangeValue)

{

onchangeValue = true;

}

else

{

txtonchangeValue = false;

if (!string.IsN

ullOrEmpty(p_txtDateTime.Text))

onchangeValue = true;

}

p_txtDateTime.Text = this.Text;

}

base.OnValueChanged(eventargs);

}

*New properties extra.

I add three new properties for this extra control is

protected bool p_AlertWrongDate = false;

protected bool p_NullableValue = true;

protected string p_ErrorMessage = "Invalid Date Format!";

p_AlertWrongDate: Is use for the choice alert or not alert the error value of date is

inputted by user.

p_NullableValue: Is use for the choice make the control can have null value or not

(if p_NullableValue=false the extra control will like the base

DateTimePicker control).

p_ErrorMessage: Allow user cans custom the content of Error message.

Code:

///

/// Show message box when input date not valid

///

public bool ShowError

{

get { return p_AlertWrongDate; }

set

{

if (!NullableValue)

{

p_AlertWrongDate = false;

}

else

{

p_AlertWrongDate = value;

}

}

}

///

/// Get or set NullableValue

///

public bool NullableValue

{

get { return p_NullableValue; }

set

{

p_NullableValue = value;

NullableValueChange();

}

}

///

/// Event will fire when the NullableValue changed

///

protected void NullableValueChange()

{

if (p_NullableValue)

{

p_txtDateTime.ReadOnly = true;

p_txtDateTime.BackColor = Color.White;

p_txtDateTime.KeyDown +=

new KeyEventHandler(p_txtDateTime_KeyDown);

p_txtDateTime.LostFocus +=

new EventHandler(p_txtDateTime_LostFocus);

p_txtDateTime.Width = this.Width - 34;

p_txtDateTime.Top = -1;

this.Controls.Add(p_txtDateTime);

initWidth = this.Width;

}

else if (this.Controls.Contains(p_txtDateTime))

{

this.ShowError = false;

this.Controls.Remove(p_txtDateTime);

}

}

///

/// Get or set ErrorMessage property

///

public string ErrorMessage

{

get { return p_ErrorMessage; }

set { p_ErrorMessage = value; }

}

Beside that I have to write some event that allows we can input value by type into the text box.

And the last I have a control like that

Download soucecode: http://www.mediafire.com/download.php?idm0ynw2idtxjkm

Không có nhận xét nào:

Đăng nhận xét