DevExpress之时间控件
dateEdit和timeEdit
基本属性
DisplayFormat.FormatString-------失去焦点是控件显示的格式,timeEdit用不上
EditMask--------------------------获取焦点时也就是在编辑状态时控件的显示格式
ShowToday-----------------------控件编辑状态时是否在编辑框中显示当天日期,dateEdit的控件
Text------------------------------获取控件的值
SelectedText---------------------获取选中的值,一般是用在timeEdit,timeEdit编辑状态时可以选中 小时/分钟/秒
显示到天
dateEdit1.Properties.DisplayFormat.FormatString = "yyyy-MM-dd"; dateEdit1.Properties.EditMask = "yyyy-MM-dd";
显示到月
dateEdit1.Properties.DisplayFormat.FormatString = "yyyy-MM"; dateEdit1.Properties.EditMask = "yyyy-MM";
显示到年
dateEdit1.Properties.DisplayFormat.FormatString = "yyyy"; dateEdit1.Properties.EditMask = "yyyy";
字符串转DateTime
DateTime.Parse("2016-3-16 12:12:12")
字符串根据指定格式转换为DateTime
IFormatProvider ifp = new CultureInfo("zh-CN", true); DateTime.ParseExact("20160316121212", "yyyyMMddHHmmss", ifp);
ifp是一个区域特定格式字符串信息
注意:大写MM是指月,小写mm是指分钟,小时二十四制要用大写HH,十二小时制用小写hh
DateTime转换指定格式字符串
DateTime datetime = DateTime.Now; string timeString = datetime.ToString("yyyyMMddHHmmss");
旧版本在显示到月或者是年的时候有点麻烦,需要自己重载dateEdit控件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | 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); } } } |
这是网上的一个例子,原文链接https://www.devexpress.com/Support/Center/Question/Details/CQ60337/control-for-selecting-month,下面这个是我稍微改了一下,只显示年面板的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | using System; using System.Collections.Generic; using System.Linq; using System.Text; 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" ; Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime; Properties.Mask.EditMask = "yyyy" ; 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.YearsInfo; } 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; //DateTime dt = new DateTime(DateTime.Year, 1, 1, 0, 0, 0); OnDateTimeCommit(cell.Date, false ); //base.OnItemClick(hitInfo); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具