DateTimeCellEditor.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. //#########################################################################################
  2. //★★★★★★★ http://www.cnpopsoft.com [华普软件] ★★★★★★★
  3. //★★★★★★★ 华普软件 - VB & C#.NET 专业论文与源码荟萃! ★★★★★★★
  4. //#########################################################################################
  5. /*
  6. * Copyright ?2005, Mathew Hall
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  25. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. * OF SUCH DAMAGE.
  29. */
  30. using System;
  31. using System.Drawing;
  32. using System.Windows.Forms;
  33. using XPTable.Models;
  34. using XPTable.Renderers;
  35. namespace XPTable.Editors
  36. {
  37. /// <summary>
  38. /// A class for editing Cells that contain DateTimes
  39. /// </summary>
  40. public class DateTimeCellEditor : DropDownCellEditor
  41. {
  42. #region EventHandlers
  43. /// <summary>
  44. /// Occurs when the user makes an explicit date selection using the mouse
  45. /// </summary>
  46. public event DateRangeEventHandler DateSelected;
  47. #endregion
  48. #region Class Data
  49. /// <summary>
  50. /// The MonthCalendar that will be shown in the drop-down portion of the
  51. /// DateTimeCellEditor
  52. /// </summary>
  53. private MonthCalendar calendar;
  54. #endregion
  55. #region Constructor
  56. /// <summary>
  57. /// Initializes a new instance of the DateTimeCellEditor class with default settings
  58. /// </summary>
  59. public DateTimeCellEditor() : base()
  60. {
  61. this.calendar = new MonthCalendar();
  62. this.calendar.Location = new System.Drawing.Point(0, 0);
  63. this.calendar.MaxSelectionCount = 1;
  64. this.DropDown.Width = this.calendar.Width + 2;
  65. this.DropDown.Height = this.calendar.Height + 2;
  66. this.DropDown.Control = this.calendar;
  67. base.DropDownStyle = DropDownStyle.DropDownList;
  68. }
  69. #endregion
  70. #region Methods
  71. /// <summary>
  72. /// Sets the location and size of the CellEditor
  73. /// </summary>
  74. /// <param name="cellRect">A Rectangle that represents the size and location
  75. /// of the Cell being edited</param>
  76. protected override void SetEditLocation(Rectangle cellRect)
  77. {
  78. // calc the size of the textbox
  79. ICellRenderer renderer = this.EditingTable.ColumnModel.GetCellRenderer(this.EditingCellPos.Column);
  80. int buttonWidth = ((DateTimeCellRenderer) renderer).ButtonWidth;
  81. this.TextBox.Size = new Size(cellRect.Width - 1 - buttonWidth, cellRect.Height-1);
  82. this.TextBox.Location = cellRect.Location;
  83. }
  84. /// <summary>
  85. /// Sets the initial value of the editor based on the contents of
  86. /// the Cell being edited
  87. /// </summary>
  88. protected override void SetEditValue()
  89. {
  90. // set default values incase we can't find what we're looking for
  91. DateTime date = DateTime.Now;
  92. String format = DateTimeColumn.LongDateFormat;
  93. if (this.EditingCell.Data != null && this.EditingCell.Data is DateTime)
  94. {
  95. date = (DateTime) this.EditingCell.Data;
  96. if (this.EditingTable.ColumnModel.Columns[this.EditingCellPos.Column] is DateTimeColumn)
  97. {
  98. DateTimeColumn dtCol = (DateTimeColumn) this.EditingTable.ColumnModel.Columns[this.EditingCellPos.Column];
  99. switch (dtCol.DateTimeFormat)
  100. {
  101. case DateTimePickerFormat.Short:
  102. format = DateTimeColumn.ShortDateFormat;
  103. break;
  104. case DateTimePickerFormat.Time:
  105. format = DateTimeColumn.TimeFormat;
  106. break;
  107. case DateTimePickerFormat.Custom:
  108. format = dtCol.CustomDateTimeFormat;
  109. break;
  110. }
  111. }
  112. }
  113. this.calendar.SelectionStart = date;
  114. this.TextBox.Text = date.ToString(format);
  115. }
  116. /// <summary>
  117. /// Sets the contents of the Cell being edited based on the value
  118. /// in the editor
  119. /// </summary>
  120. protected override void SetCellValue()
  121. {
  122. this.EditingCell.Data = this.calendar.SelectionStart;
  123. }
  124. /// <summary>
  125. /// Starts editing the Cell
  126. /// </summary>
  127. public override void StartEditing()
  128. {
  129. this.calendar.DateSelected += new DateRangeEventHandler(calendar_DateSelected);
  130. this.TextBox.SelectionLength = 0;
  131. base.StartEditing();
  132. }
  133. /// <summary>
  134. /// Stops editing the Cell and commits any changes
  135. /// </summary>
  136. public override void StopEditing()
  137. {
  138. this.calendar.DateSelected -= new DateRangeEventHandler(calendar_DateSelected);
  139. base.StopEditing();
  140. }
  141. /// <summary>
  142. /// Stops editing the Cell and ignores any changes
  143. /// </summary>
  144. public override void CancelEditing()
  145. {
  146. this.calendar.DateSelected -= new DateRangeEventHandler(calendar_DateSelected);
  147. base.CancelEditing();
  148. }
  149. #endregion
  150. #region Properties
  151. /// <summary>
  152. /// Gets or sets a value specifying the style of the drop down editor
  153. /// </summary>
  154. public new DropDownStyle DropDownStyle
  155. {
  156. get
  157. {
  158. return base.DropDownStyle;
  159. }
  160. set
  161. {
  162. throw new NotSupportedException();
  163. }
  164. }
  165. #endregion
  166. #region Events
  167. /// <summary>
  168. /// Raises the DateSelected event
  169. /// </summary>
  170. /// <param name="e">A DateRangeEventArgs that contains the event data</param>
  171. protected virtual void OnDateSelected(DateRangeEventArgs e)
  172. {
  173. if (DateSelected != null)
  174. {
  175. DateSelected(this, e);
  176. }
  177. }
  178. /// <summary>
  179. /// Handler for the editors MonthCalendar.DateSelected events
  180. /// </summary>
  181. /// <param name="sender">The object that raised the event</param>
  182. /// <param name="e">A DateRangeEventArgs that contains the event data</param>
  183. private void calendar_DateSelected(object sender, DateRangeEventArgs e)
  184. {
  185. this.DroppedDown = false;
  186. this.OnDateSelected(e);
  187. this.EditingTable.StopEditing();
  188. }
  189. #endregion
  190. }
  191. }