TextCellEditor.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.Events;
  34. using XPTable.Models;
  35. using XPTable.Win32;
  36. namespace XPTable.Editors
  37. {
  38. /// <summary>
  39. /// A class for editing Cells that contain strings
  40. /// </summary>
  41. public class TextCellEditor : CellEditor
  42. {
  43. #region Constructor
  44. /// <summary>
  45. /// Initializes a new instance of the TextCellEditor class with default settings
  46. /// </summary>
  47. public TextCellEditor() : base()
  48. {
  49. TextBox textbox = new TextBox();
  50. textbox.AutoSize = false;
  51. textbox.BorderStyle = BorderStyle.None;
  52. this.Control = textbox;
  53. }
  54. #endregion
  55. #region Methods
  56. /// <summary>
  57. /// Sets the location and size of the CellEditor
  58. /// </summary>
  59. /// <param name="cellRect">A Rectangle that represents the size and location
  60. /// of the Cell being edited</param>
  61. protected override void SetEditLocation(Rectangle cellRect)
  62. {
  63. this.TextBox.Location = cellRect.Location;
  64. this.TextBox.Size = new Size(cellRect.Width-1, cellRect.Height-1);
  65. }
  66. /// <summary>
  67. /// Sets the initial value of the editor based on the contents of
  68. /// the Cell being edited
  69. /// </summary>
  70. protected override void SetEditValue()
  71. {
  72. this.TextBox.Text = this.EditingCell.Text;
  73. }
  74. /// <summary>
  75. /// Sets the contents of the Cell being edited based on the value
  76. /// in the editor
  77. /// </summary>
  78. protected override void SetCellValue()
  79. {
  80. this.EditingCell.Text = this.TextBox.Text;
  81. }
  82. /// <summary>
  83. /// Starts editing the Cell
  84. /// </summary>
  85. public override void StartEditing()
  86. {
  87. this.TextBox.KeyPress += new KeyPressEventHandler(OnKeyPress);
  88. this.TextBox.LostFocus += new EventHandler(OnLostFocus);
  89. base.StartEditing();
  90. this.TextBox.Focus();
  91. }
  92. /// <summary>
  93. /// Stops editing the Cell and commits any changes
  94. /// </summary>
  95. public override void StopEditing()
  96. {
  97. this.TextBox.KeyPress -= new KeyPressEventHandler(OnKeyPress);
  98. this.TextBox.LostFocus -= new EventHandler(OnLostFocus);
  99. base.StopEditing();
  100. }
  101. /// <summary>
  102. /// Stops editing the Cell and ignores any changes
  103. /// </summary>
  104. public override void CancelEditing()
  105. {
  106. this.TextBox.KeyPress -= new KeyPressEventHandler(OnKeyPress);
  107. this.TextBox.LostFocus -= new EventHandler(OnLostFocus);
  108. base.CancelEditing();
  109. }
  110. #endregion
  111. #region Properties
  112. /// <summary>
  113. /// Gets the TextBox used to edit the Cells contents
  114. /// </summary>
  115. public TextBox TextBox
  116. {
  117. get
  118. {
  119. return this.Control as TextBox;
  120. }
  121. }
  122. #endregion
  123. #region Events
  124. /// <summary>
  125. /// Handler for the editors TextBox.KeyPress event
  126. /// </summary>
  127. /// <param name="sender">The object that raised the event</param>
  128. /// <param name="e">A KeyPressEventArgs that contains the event data</param>
  129. protected virtual void OnKeyPress(object sender, KeyPressEventArgs e)
  130. {
  131. if (e.KeyChar == AsciiChars.CarriageReturn /*Enter*/)
  132. {
  133. if (this.EditingTable != null)
  134. {
  135. this.EditingTable.StopEditing();
  136. }
  137. }
  138. else if (e.KeyChar == AsciiChars.Escape)
  139. {
  140. if (this.EditingTable != null)
  141. {
  142. this.EditingTable.CancelEditing();
  143. }
  144. }
  145. }
  146. /// <summary>
  147. /// Handler for the editors TextBox.LostFocus event
  148. /// </summary>
  149. /// <param name="sender">The object that raised the event</param>
  150. /// <param name="e">An EventArgs that contains the event data</param>
  151. protected virtual void OnLostFocus(object sender, EventArgs e)
  152. {
  153. if (this.EditingTable != null)
  154. {
  155. this.EditingTable.StopEditing();
  156. }
  157. }
  158. #endregion
  159. }
  160. }