zoukankan      html  css  js  c++  java
  • Devexpress DateEdit选年月

    Devexpress DateEdit选年月

    经常遇到需要在WinForm中选择年月的需求,用Devexpress本身的控件DataEdit无法实现,即使设置Mask与Display只显示年月2012-02这种格式,但用户选择起来还是不爽,体验太差。

    最后我写了一个类,一些代码源自 http://www.devexpress.com/Support/Center/p/CQ60337.aspx

    效果如下:




    代码:

     using Microsoft.VisualBasic;

    using System;

    using System.Collections;

    using System.Collections.Generic;

    using System.Data;

    using System.Diagnostics;

    using DevExpress.XtraEditors;

    using DevExpress.XtraEditors.Calendar;

    using DevExpress.XtraEditors.Popup;

    using DevExpress.XtraEditors.Controls;

    using DevExpress.XtraEditors.Repository;

     namespace LZJA.Common

    {

        public class DateEditEx : DateEdit

        {

            public DateEditEx()

            {

                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 DateEditEx.DateResultModeEnum DateMode

            {

                get { return ((DateEditEx)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 == DateEditEx.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);

            }

        }

    }

  • 相关阅读:
    Lightmaping
    Android内存回收机制
    基本光照模型简单实现
    Pass的通用指令开关
    使用Depth Texture
    使用替换shader渲染
    Windows下安装Oracle12C(一)
    SpringMVC文件上传基础
    Spring集成线程池
    《经久不衰的Spring框架:@ResponseBody 中文乱码》(转)
  • 原文地址:https://www.cnblogs.com/xuhaibiao/p/2653702.html
Copyright © 2011-2022 走看看