//######################################################################################### //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★ //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★ //######################################################################################### /* * Copyright ?2005, Mathew Hall * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. */ using System; using System.Drawing; using System.Windows.Forms; using XPTable.Models; using XPTable.Renderers; namespace XPTable.Editors { /// /// A class for editing Cells that contain DateTimes /// public class DateTimeCellEditor : DropDownCellEditor { #region EventHandlers /// /// Occurs when the user makes an explicit date selection using the mouse /// public event DateRangeEventHandler DateSelected; #endregion #region Class Data /// /// The MonthCalendar that will be shown in the drop-down portion of the /// DateTimeCellEditor /// private MonthCalendar calendar; #endregion #region Constructor /// /// Initializes a new instance of the DateTimeCellEditor class with default settings /// public DateTimeCellEditor() : base() { this.calendar = new MonthCalendar(); this.calendar.Location = new System.Drawing.Point(0, 0); this.calendar.MaxSelectionCount = 1; this.DropDown.Width = this.calendar.Width + 2; this.DropDown.Height = this.calendar.Height + 2; this.DropDown.Control = this.calendar; base.DropDownStyle = DropDownStyle.DropDownList; } #endregion #region Methods /// /// Sets the location and size of the CellEditor /// /// A Rectangle that represents the size and location /// of the Cell being edited protected override void SetEditLocation(Rectangle cellRect) { // calc the size of the textbox ICellRenderer renderer = this.EditingTable.ColumnModel.GetCellRenderer(this.EditingCellPos.Column); int buttonWidth = ((DateTimeCellRenderer) renderer).ButtonWidth; this.TextBox.Size = new Size(cellRect.Width - 1 - buttonWidth, cellRect.Height-1); this.TextBox.Location = cellRect.Location; } /// /// Sets the initial value of the editor based on the contents of /// the Cell being edited /// protected override void SetEditValue() { // set default values incase we can't find what we're looking for DateTime date = DateTime.Now; String format = DateTimeColumn.LongDateFormat; if (this.EditingCell.Data != null && this.EditingCell.Data is DateTime) { date = (DateTime) this.EditingCell.Data; if (this.EditingTable.ColumnModel.Columns[this.EditingCellPos.Column] is DateTimeColumn) { DateTimeColumn dtCol = (DateTimeColumn) this.EditingTable.ColumnModel.Columns[this.EditingCellPos.Column]; switch (dtCol.DateTimeFormat) { case DateTimePickerFormat.Short: format = DateTimeColumn.ShortDateFormat; break; case DateTimePickerFormat.Time: format = DateTimeColumn.TimeFormat; break; case DateTimePickerFormat.Custom: format = dtCol.CustomDateTimeFormat; break; } } } this.calendar.SelectionStart = date; this.TextBox.Text = date.ToString(format); } /// /// Sets the contents of the Cell being edited based on the value /// in the editor /// protected override void SetCellValue() { this.EditingCell.Data = this.calendar.SelectionStart; } /// /// Starts editing the Cell /// public override void StartEditing() { this.calendar.DateSelected += new DateRangeEventHandler(calendar_DateSelected); this.TextBox.SelectionLength = 0; base.StartEditing(); } /// /// Stops editing the Cell and commits any changes /// public override void StopEditing() { this.calendar.DateSelected -= new DateRangeEventHandler(calendar_DateSelected); base.StopEditing(); } /// /// Stops editing the Cell and ignores any changes /// public override void CancelEditing() { this.calendar.DateSelected -= new DateRangeEventHandler(calendar_DateSelected); base.CancelEditing(); } #endregion #region Properties /// /// Gets or sets a value specifying the style of the drop down editor /// public new DropDownStyle DropDownStyle { get { return base.DropDownStyle; } set { throw new NotSupportedException(); } } #endregion #region Events /// /// Raises the DateSelected event /// /// A DateRangeEventArgs that contains the event data protected virtual void OnDateSelected(DateRangeEventArgs e) { if (DateSelected != null) { DateSelected(this, e); } } /// /// Handler for the editors MonthCalendar.DateSelected events /// /// The object that raised the event /// A DateRangeEventArgs that contains the event data private void calendar_DateSelected(object sender, DateRangeEventArgs e) { this.DroppedDown = false; this.OnDateSelected(e); this.EditingTable.StopEditing(); } #endregion } }