Skip to content

Instantly share code, notes, and snippets.

@hsinjungwu
Last active August 29, 2015 14:03
Show Gist options
  • Save hsinjungwu/74e2bdc994c783fb3f7e to your computer and use it in GitHub Desktop.
Save hsinjungwu/74e2bdc994c783fb3f7e to your computer and use it in GitHub Desktop.
DevExpress DateEditor Popout YearMonth View
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>
/// DateEditor that display year-month and only can select year-month view
/// </summary>
public class DateEditYearMonth : DateEdit
{
public int StoredDate { get; set; }
public DateEditYearMonth(DateEdit de, int storedDate)
: base()
{
(de.Parent as Control).Controls.Add(this);
Location = de.Location;
Size = de.Size;
//This can avoid dropdown error when in Windows XP
Properties.VistaDisplayMode = DevExpress.Utils.DefaultBoolean.True;
Properties.AppearanceFocused.BackColor = de.Properties.AppearanceFocused.BackColor;
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;
Properties.NullDate = de.Properties.NullDate = MinDateTime;
if (de.DateTime == MinDateTime) EditValue = null;
else DateTime = de.DateTime;
StoredDate = storedDate;
KeyDown += (s, e) =>
{
if (e.KeyCode == Keys.Delete) //use `DELETE` to clear
{
if (Properties.ReadOnly)
e.Handled = false;
else
{
CancelPopup();
EditValue = MinDateTime;
e.Handled = true;
}
}
};
}
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>
/// Set assigned DateEditor only display year-month and on can select year-month view. Moreover, you can assigned stored date
/// </summary>
/// <param name="de">assigned DateEditor</param>
/// <param name="date">default is 1. If -1, that means the last date of month</param>
public static void SetYearMonth(this DateEdit de, int date = 1)
{
DateEditYearMonth deym = new DateEditYearMonth(de, date);
de.Visible = false;
de.EnabledChanged += (s, e) => { deym.Enabled = de.Enabled; };
de.PropertiesChanged += (s, e) =>
{
deym.Properties.ReadOnly = de.Properties.ReadOnly;
deym.DateTime = de.DateTime;
};
de.EditValueChanged += (s, e) =>
{
if (deym.Properties.ReadOnly) deym.DateTime = de.DateTime;
else
{
if (de.IsEmpty()) deym.DateTime = MinDateTime;
//Avoid scroll or type date to cause error
if (deym.EditValue == de.EditValue) return;
DateTime lastDate = MyDateTimeFuncs.LastDayOfMonth(deym.DateTime);
if (date == -1)
{
de.EditValue = lastDate;
return;
}
DateTime firstDate = MyDateTimeFuncs.FirstDayOfMonth(deym.DateTime);
DateTime queryDate = firstDate.AddDays(date - 1);
if (queryDate > lastDate) de.EditValue = lastDate;
else de.EditValue = queryDate;
}
};
deym.EditValueChanged += (s, e) =>
{
if (deym.Properties.ReadOnly || deym.IsEmpty()) return;
de.DateTime = deym.DateTime;
};
}
/// <summary>
/// Set girdcolumn only display year-month
/// </summary>
/// <param name="colTime">assigned Girdcolumn</param>
public static void DisplayYearMonthFormat(GridColumn colTime)
{
RepositoryItemDateEdit ride = new RepositoryItemDateEdit();
ride.Mask.Culture = My.CultureName;
ride.Mask.EditMask = My.DateMask;
ride.NullDate = MinDateTime;
ride.MinValue = MinDateTime;
colTime.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
colTime.DisplayFormat.FormatString = "y";
ride.Mask.UseMaskAsDisplayFormat = false;
colTime.ColumnEdit = ride;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment