Skip to content

Instantly share code, notes, and snippets.

@LSTANCZYK
Forked from hsinjungwu/YearMonthEditor.cs
Last active August 29, 2015 14:07
Show Gist options
  • Save LSTANCZYK/57398ec1f78b2617b2a6 to your computer and use it in GitHub Desktop.
Save LSTANCZYK/57398ec1f78b2617b2a6 to your computer and use it in GitHub Desktop.
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Calendar;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraEditors.Repository;
DateTime MinDateTime = new DateTime(1900, 1, 1);
/// <summary>
/// 只顯示年月,且選擇時也只能選擇年月的 dateEdit
/// </summary>
public class YearMonthEdit : DateEdit
{
private int _storedDate = 1;
public int storedDate
{
get { return this._storedDate; }
set { this._storedDate = value; }
}
public YearMonthEdit()
{
Properties.Mask.EditMask = "y";
Properties.DisplayFormat.FormatString = "y";
Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
Properties.EditFormat.FormatString = "y";
Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
}
public YearMonthEdit(DateEdit de) : this()
{
(de.Parent as System.Windows.Forms.Control).Controls.Add(this);
Location = de.Location;
Size = de.Size;
if (de.DateTime == MinDateTime) EditValue = null;
else DateTime = de.DateTime;
}
protected override PopupBaseForm CreatePopupForm()
{
return new YearMonthVistaPopupDateEditForm(this, _storedDate);
}
protected override void CreateRepositoryItem()
{
fProperties = new YearMonthRepositoryItemDateEdit(this);
}
private class YearMonthRepositoryItemDateEdit : RepositoryItemDateEdit
{
public YearMonthRepositoryItemDateEdit(DateEdit dateEdit)
{
SetOwnerEdit(dateEdit);
}
public override bool ShowToday
{
get{ return false; }
}
}
private class YearMonthVistaPopupDateEditForm : VistaPopupDateEditForm
{
private int _storedDate { get; set; }
public YearMonthVistaPopupDateEditForm(DateEdit ownerEdit, int storedDate) : base(ownerEdit)
{
this._storedDate = storedDate;
}
protected override DateEditCalendar CreateCalendar()
{
return new YearMonthVistaDateEditCalendar(OwnerEdit.Properties,OwnerEdit.EditValue, _storedDate);
}
}
private class YearMonthVistaDateEditCalendar : VistaDateEditCalendar
{
private int _storedDate { get; set; }
public YearMonthVistaDateEditCalendar(RepositoryItemDateEdit item, object editDate, int storedDate) : base(item, editDate)
{
_storedDate = storedDate;
}
protected override void Init()
{
base.Init();
View = DateEditCalendarViewType.YearInfo;
}
protected override void OnItemClick(CalendarHitInfo hitInfo)
{
DayNumberCellInfo cell = (DayNumberCellInfo)hitInfo.HitObject;
if (View == DateEditCalendarViewType.YearInfo)
{
DateTime date;
if (_storedDate > 0) date = new DateTime(DateTime.Year, cell.Date.Month, _storedDate);
else date = DateTimeFuncs.LastDayOfMonth(new DateTime(DateTime.Year, cell.Date.Month, 1));
OnDateTimeCommit(date, false);
}
else
{
base.OnItemClick(hitInfo);
}
}
}
}
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
DateTime MinDateTime = new DateTime(1900, 1, 1);
/// <summary>
/// 讓指定的 DateEdit 只顯示年月,且選擇時也只能選擇年月。可指定儲存日期。
/// </summary>
/// <param name="de">指定的 DateEdit</param>
/// <param name="date">若為 -1 表示月底</param>
public static void SetYearMonth(DateEdit de, int date)
{
YearMonthEdit deym = new YearMonthEdit(de);
deym.storedDate = date;
deym.Properties.NullDate = MinDateTime;
de.Visible = false;
de.Properties.NullDate = MinDateTime;
de.EnabledChanged += (s, e) => deym.Enabled = de.Enabled;
de.PropertiesChanged += (s, e) => deym.Properties.ReadOnly = de.Properties.ReadOnly;
deym.EditValueChanged += (s, e) => de.EditValue = deym.EditValue;
}
/// <summary>
/// 設定 Girdcolumn 只顯示年月
/// </summary>
/// <param name="colDate">指定之 Girdcolumn</param>
public static void DisplayYearMonthFormat(GridColumn colDate)
{
RepositoryItemDateEdit ride = new RepositoryItemDateEdit();
ride.DisplayFormat.FormatString = "y";
ride.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
ride.EditFormat.FormatString = "y";
ride.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
ride.Mask.EditMask = "y";
colDate.ColumnEdit = ride;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment