重写DEV的DateEdit控件的类只选择年月

  最新在做CRM的报表,查询条件只需要年月,DateEdit 以及几个时间控件都用的不顺,强迫症犯了一怒之下起了重写DateEdit的想法

新建一个类

  CXDateEdit 

using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Calendar;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraEditors.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MLTY.Business.Report
{
   public class CXDateEdit : DateEdit
    {
        public CXDateEdit()
        {
            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;
        }
 
        protected override PopupBaseForm CreatePopupForm()
 
        {
 
            if (Properties.VistaDisplayMode == DevExpress.Utils.DefaultBoolean.True)
 
                return new CustomVistaPopupDateEditForm(this);
 
            return new PopupDateEditForm(this);
 
        }
 
        private DateResultModeEnum _dateMode = DateResultModeEnum.FirstDayOfMonth;
 
        public DateResultModeEnum DateMode
 
        {
 
            get { return _dateMode; }
 
            set { _dateMode = value; }
 
        }
 
 
 
        public enum DateResultModeEnum : int
 
        {
 
            //虽然是年月控件,但日期Datetime肯定是2012-01-01这种格式
 
            //所以,这个枚举定义了年月控件返回本月的第一天,还是本月的最后一天作为DateEditEx的值
 
            FirstDayOfMonth = 1,
 
            LastDayOfMonth = 2
 
        } 
 
 
 
    }
 
    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;
 
        }
 
        public CXDateEdit.DateResultModeEnum DateMode
 
        {
 
            get { return ((CXDateEdit)this.Properties.OwnerEdit).DateMode; }
 
        }
 
        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, 1, 0, 0, 0);
 
                if (DateMode == CXDateEdit.DateResultModeEnum.FirstDayOfMonth)
 
                {
 
                    
 
                    OnDateTimeCommit(dt, false);
 
                }
 
                else
 
                {
 
                    DateTime tempDate = dt.AddMonths(1).AddDays(-1);
 
                    tempDate = new DateTime(tempDate.Year, tempDate.Month, tempDate.Day, 23, 59, 59);
 
                    OnDateTimeCommit(tempDate, false);
 
                }
 
            }
 
            else
 
                base.OnItemClick(hitInfo);
 
        }
 
    }
 
}

  然后在窗体xxx上拖一个dateedit ,进入xxx.Designer.cs 将设计器代码 this.dateedit =new DevExpress.XtraEditors.DateEdit();

改成 重写的类

原博地址:http://www.cnblogs.com/axing/p/3201066.html

 

转载于:https://www.cnblogs.com/li-lun/p/4757349.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值