SunnyUI/SunnyUI/Controls/UIDatePicker.cs

89 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/******************************************************************************
* SunnyUI 开源控件库、工具类库、扩展类库、多页面开发框架。
* CopyRight (C) 2012-2020 ShenYongHua(沈永华).
* QQ群56829229 QQ17612584 EMailSunnyUI@qq.com
*
* Blog: https://www.cnblogs.com/yhuse
* Gitee: https://gitee.com/yhuse/SunnyUI
* GitHub: https://github.com/yhuse/SunnyUI
*
* SunnyUI.dll can be used for free under the GPL-3.0 license.
* If you use this code, please keep this note.
* 如果您使用此代码,请保留此说明。
******************************************************************************
* 文件名称: UIDatePicker.cs
* 文件说明: 日期选择框
* 当前版本: V2.2
* 创建日期: 2020-01-01
*
* 2020-01-01: V2.2.0 增加文件说明
******************************************************************************/
using System;
using System.ComponentModel;
namespace Sunny.UI
{
[ToolboxItem(true)]
[DefaultProperty("Value")]
[DefaultEvent("ValueChanged")]
public sealed partial class UIDatePicker : UIDropControl
{
public delegate void OnDateTimeChanged(object sender, DateTime value);
public UIDatePicker()
{
InitializeComponent();
Value = DateTime.Now;
}
public event OnDateTimeChanged ValueChanged;
protected override void ItemForm_ValueChanged(object sender, object value)
{
Value = (DateTime)value;
Text = Value.ToString(dateFormat);
Invalidate();
ValueChanged?.Invoke(this, Value);
}
private readonly UIDateItem item = new UIDateItem();
protected override void CreateInstance()
{
ItemForm = new UIDropDown(item);
}
public DateTime Value
{
get => item.Date;
set
{
if (value< new DateTime(1753,1,1))
value = new DateTime(1753,1,1);
Text = value.ToString(dateFormat);
item.Date = value;
}
}
private void UIDatetimePicker_ButtonClick(object sender, EventArgs e)
{
item.Date = Value;
ItemForm.Show(this);
}
private string dateFormat = "yyyy-MM-dd";
[Description("日期格式化掩码"), Category("自定义")]
[DefaultValue("yyyy-MM-dd")]
public string DateFormat
{
get => dateFormat;
set
{
dateFormat = value;
Text = Value.ToString(dateFormat);
}
}
}
}