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控件
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,下面這個是我稍微改了一下,只顯示年面板的代碼
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); } } }