玩转控件:重绘DEVEXPRESS中DateEdit控件 —— 让DateEdit支持只选择年月 (提供源码下载)

时间:2021-05-30 20:22:33
  前言

上一篇博文《玩转控件:重绘ComboBox —— 让ComboBox多列显示》中,根据大家的回馈,ComboBox已经支持筛选了,更新见博文最后最后最后面.

  奇葩

这两天遇到一个很“诡异”的情况,之前手机拦截短信一直用腾讯手机管家,这两天发神经,把腾讯手机管家卸载掉,换成360手机管家。然后一如既往的设置好闹钟,睡觉.. 结果尼玛一觉睡到自然醒!拿过手机一看11:35am,手机闹钟居然没响!我很希望今天是星期六,为此我还狠劲的捏了下我白里透红的小脸,却无法扭转今天星期五的窘境。 这还不是最奇葩的,奇葩的是,我拿起手机惊奇的发现:360不见了...腾讯也不见了...闹钟也不响了...我凌乱了. 不知道有没有人遇到过类似的问题、

  缘由

这两天在维护DevExpress控件开发的报表,测试人员提出报表查询条件需要按月查询,我木讷的对着Dev自带的DateEdit控件许久,又找了几个Dev其他的时间控件,没有一个可以达到需求标准,于是有了下文:本篇博文代码部分来自网络,地址:http://www.devexpress.com/Support/Center/Question/Details/CQ60337

  故技重施:新建组件

根据最近看了3遍的《新三国演义》中,诸葛亮占荆州后收取零陵时,一屠夫的话:"说出吾名,吓汝一跳"。随将该组件类前缀用鄙人姓名字母的缩写,使其继承DevExpress的DateEdit控件类,如下:

  public partial class KzxDateEdit : DateEdit

我用的DevExpress控件是v10.1的,经过测试DevExpress神似在v7.1以上才支持Vista模式.(此处未经考证,如官方人员发现,请将此言论归为 大言不惭 之类别!)

下面贴出核心代码:

 public partial class KzxDateEdit : DateEdit
{
public KzxDateEdit()
{
InitializeComponent();
Properties.VistaDisplayMode = DevExpress.Utils.DefaultBoolean.True;
Properties.DisplayFormat.FormatString = "yyyy-MM";
Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
Properties.Mask.EditMask = "yyyy-MM";
Properties.ShowToday = false;
} public KzxDateEdit(IContainer container)
{
container.Add(this); InitializeComponent();
}
protected override PopupBaseForm CreatePopupForm()
{
if (Properties.VistaDisplayMode == DevExpress.Utils.DefaultBoolean.True)
return new CustomVistaPopupDateEditForm(this);
return new PopupDateEditForm(this);
} }
public class CustomVistaPopupDateEditForm : VistaPopupDateEditForm
{
public CustomVistaPopupDateEditForm(DateEdit ownerEdit) : base(ownerEdit) { }
protected override DateEditCalendar CreateCalendar()
{
return new CustomVistaDateEditCalendar(OwnerEdit.Properties, OwnerEdit.EditValue);
}
}
public class CustomVistaDateEditCalendar : VistaDateEditCalendar
{
public CustomVistaDateEditCalendar(RepositoryItemDateEdit item, object editDate) : base(item, editDate) { } protected override void Init()
{
base.Init();
this.View = DateEditCalendarViewType.YearInfo;
} protected override void OnItemClick(DevExpress.XtraEditors.Calendar.CalendarHitInfo hitInfo)
{
DayNumberCellInfo cell = hitInfo.HitObject as DayNumberCellInfo;
if (View == DateEditCalendarViewType.YearInfo)
{
DateTime dt = new DateTime(DateTime.Year, cell.Date.Month, , , , ); DateTime tempDate = dt.AddMonths().AddDays(-);
tempDate = new DateTime(tempDate.Year, tempDate.Month, tempDate.Day, , , );
OnDateTimeCommit(tempDate, false);
}
else
base.OnItemClick(hitInfo);
}
}
   回到窗体,重新定义窗体设计器的代码InitializeComponent()

没错,仍然需要使出拖控件大法,将DevExpress中DateEdit控件拖到窗体中,然后将窗体的设计代码中InitializeComponent()方法剪切到窗体的.cs文件中,然后重新制定该DateEdit控件实例化对象即可。

 //this.dateEdit1 = new DevExpress.XtraEditors.DateEdit();
this.dateEdit1 = new KzxDateEdit();

下面是运行效果图:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARsAAAEQCAIAAAAPiEe1AAASG0lEQVR4nO2deXhU5b2Av7+uXq1KXVC53lt7L6XgUq2WumAVqx2t1rW2LlUrCi6sAhEEQRBFIJCENew7g2EXoSwqMiIiSnsFRapFvKCyZBIIJISAJJz7x4RkMuc728zvZE4m7/u8j09ycs75Pg/nzTczkIw6rXnH5Dz9Z51Ob9HpRy06n/Hzzme27H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