Custom DateTimePicker control
I have created the user control that implements DateTimePicker
. But I have problems with Databindings
. When Control is loaded dtp.Value has default DateTime
.Now value. Why?
//Part of Form InitializeComponent() :
this.dtp_user.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.employeesBindingSource, "BirthDate", true));
// Custom Control Contains two buttons and textBox (usrText)
public partial class usrDateTimePicker : UserControl
{
private DateTimePicker dtp;
private bool _cheked;
public bool Checked
{
get { return _cheked; }
set { _cheked = value; }
}
public DateTime Value
{
get { return dtp.Value; }
set
{
if (value < DateTime.MinValue && value > DateTime.MaxValue)
value = DateTime.Now;
dtp.Value = value;
}
}
public override string Text
{
get { return usrText.Text; }
}
public ControlBindingsCollection DataBindings
{
get { return dtp.DataBindings;}
}
usrCalendar clnd;
Popup _popup;
public u开发者_开发技巧srDateTimePicker()
{
InitializeComponent();
InitStyles();
InitControls();
}
private void InitStyles()
{
//
}
private void InitControls()
{
dtp = new DateTimePicker();
clnd = new usrCalendar();
_popup = new Popup(clnd);
_popup.Closed += popup_Closed;
}
protected override void OnLoad(EventArgs e)
{
usrText.Text = FormatDate(dtp.Value);
base.OnLoad(e);
}
protected override void OnSizeChanged(EventArgs e)
{
//
}
private void btn_datetimepick_Click(object sender, EventArgs e)
{
if (!_popup.Visible)
{
_popup.Show(this);
}
else
_popup.Close();
}
private void popup_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
dtp.Value = clnd.Value;
usrText.Text = FormatDate(dtp.Value);
_cheked = true;
if (usrText.Text == string.Empty || usrText.Text == "")
_cheked = false;
}
private string FormatDate(DateTime date)
{
//
}
By default the Value
of DatePicker control is set to DateTime.Now
unless you set a value specifically. Read Here
精彩评论