using System;
using System.ComponentModel;
using System.Windows.Forms;
using FastReport.Utils;
namespace FastReport.Dialog
{
///
/// Represents a Windows control that allows the user to select a date and a time and to display the date and time with a specified format.
/// Wraps the control.
///
public partial class DateTimePickerControl : DataFilterBaseControl
{
private DateTimePicker dateTimePicker;
private string valueChangedEvent;
#region Properties
///
/// Occurs after the date has been changed.
/// Wraps the event.
///
public event EventHandler ValueChanged;
///
/// Gets an internal DateTimePicker.
///
[Browsable(false)]
public DateTimePicker DateTimePicker
{
get { return dateTimePicker; }
}
///
/// Gets or sets a value indicating whether the Value property has been set with a valid date/time value and the displayed value is able to be updated.
/// Wraps the property.
///
[DefaultValue(false)]
[Category("Behavior")]
public bool Checked
{
get { return DateTimePicker.Checked; }
set { DateTimePicker.Checked = value; }
}
///
/// Gets or sets the custom date/time format string.
/// Wraps the property.
///
[Category("Data")]
public string CustomFormat
{
get { return DateTimePicker.CustomFormat; }
set { DateTimePicker.CustomFormat = value; }
}
///
/// Gets or sets the alignment of the drop-down calendar on the DateTimePicker control.
/// Wraps the property.
///
[DefaultValue(LeftRightAlignment.Left)]
[Category("Appearance")]
public LeftRightAlignment DropDownAlign
{
get { return DateTimePicker.DropDownAlign; }
set { DateTimePicker.DropDownAlign = value; }
}
///
/// Gets or sets the format of the date and time displayed in the control.
/// Wraps the property.
///
[DefaultValue(DateTimePickerFormat.Long)]
[Category("Data")]
public DateTimePickerFormat Format
{
get { return DateTimePicker.Format; }
set { DateTimePicker.Format = value; }
}
///
/// Gets or sets the maximum date and time that can be selected in the control.
/// Wraps the property.
///
[Category("Data")]
public DateTime MaxDate
{
get { return DateTimePicker.MaxDate; }
set { DateTimePicker.MaxDate = value; }
}
///
/// Gets or sets the minimum date and time that can be selected in the control.
/// Wraps the property.
///
[Category("Data")]
public DateTime MinDate
{
get { return DateTimePicker.MinDate; }
set { DateTimePicker.MinDate = value; }
}
///
/// Gets or sets a value indicating whether a check box is displayed to the left of the selected date.
/// Wraps the property.
///
[DefaultValue(false)]
[Category("Appearance")]
public bool ShowCheckBox
{
get { return DateTimePicker.ShowCheckBox; }
set { DateTimePicker.ShowCheckBox = value; }
}
///
/// Gets or sets a value indicating whether a spin button control (also known as an up-down control) is used to adjust the date/time value.
/// Wraps the property.
///
[DefaultValue(false)]
[Category("Appearance")]
public bool ShowUpDown
{
get { return DateTimePicker.ShowUpDown; }
set { DateTimePicker.ShowUpDown = value; }
}
///
/// Gets or sets the date/time value assigned to the control.
/// Wraps the property.
///
[Category("Data")]
public DateTime Value
{
get { return DateTimePicker.Value; }
set { DateTimePicker.Value = value; }
}
///
/// Gets or sets a script method name that will be used to handle the
/// event.
///
[Category("Events")]
public string ValueChangedEvent
{
get { return valueChangedEvent; }
set { valueChangedEvent = value; }
}
#endregion
#region Private Methods
private void DateTimePicker_ValueChanged(object sender, EventArgs e)
{
OnValueChanged(e);
}
#endregion
#region Protected Methods
///
protected override void AttachEvents()
{
base.AttachEvents();
DateTimePicker.ValueChanged += DateTimePicker_ValueChanged;
}
///
protected override void DetachEvents()
{
base.DetachEvents();
DateTimePicker.ValueChanged -= DateTimePicker_ValueChanged;
}
///
protected override object GetValue()
{
if (Format == DateTimePickerFormat.Long || Format == DateTimePickerFormat.Short)
return new DateTime(Value.Year, Value.Month, Value.Day);
return Value;
}
#endregion
#region Public Methods
///
public override void Serialize(FRWriter writer)
{
DateTimePickerControl c = writer.DiffObject as DateTimePickerControl;
base.Serialize(writer);
if (Checked != c.Checked)
writer.WriteBool("Checked", Checked);
if (CustomFormat != c.CustomFormat)
writer.WriteStr("CustomFormat", CustomFormat);
if (DropDownAlign != c.DropDownAlign)
writer.WriteValue("DropDownAlign", DropDownAlign);
if (Format != c.Format)
writer.WriteValue("Format", Format);
if (MaxDate != c.MaxDate)
writer.WriteValue("MaxDate", MaxDate);
if (MinDate != c.MinDate)
writer.WriteValue("MinDate", MinDate);
if (ShowCheckBox != c.ShowCheckBox)
writer.WriteBool("ShowCheckBox", ShowCheckBox);
if (ShowUpDown != c.ShowUpDown)
writer.WriteBool("ShowUpDown", ShowUpDown);
if (Value != c.Value)
writer.WriteValue("Value", Value);
if (ValueChangedEvent != c.ValueChangedEvent)
writer.WriteStr("ValueChangedEvent", ValueChangedEvent);
}
///
/// This method fires the ValueChanged event and the script code connected to the ValueChangedEvent.
///
/// Event data.
public virtual void OnValueChanged(EventArgs e)
{
OnFilterChanged();
if (ValueChanged != null)
ValueChanged(this, e);
InvokeEvent(ValueChangedEvent, e);
}
#endregion
///
/// Initializes a new instance of the DateTimePickerControl class with default settings.
///
public DateTimePickerControl()
{
dateTimePicker = new DateTimePicker();
Control = dateTimePicker;
BindableProperty = this.GetType().GetProperty("Value");
}
}
}