NumberColumn.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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.ComponentModel;
  32. using System.Drawing;
  33. using System.Windows.Forms;
  34. using XPTable.Editors;
  35. using XPTable.Events;
  36. using XPTable.Models.Design;
  37. using XPTable.Renderers;
  38. using XPTable.Sorting;
  39. namespace XPTable.Models
  40. {
  41. /// <summary>
  42. /// Represents a Column whose Cells are displayed as a numbers
  43. /// </summary>
  44. [DesignTimeVisible(false),
  45. ToolboxItem(false)]
  46. public class NumberColumn : Column
  47. {
  48. #region Class Data
  49. /// <summary>
  50. /// The value to increment or decrement a Cell when its up or down buttons are clicked
  51. /// </summary>
  52. private decimal increment;
  53. /// <summary>
  54. /// The maximum value for a Cell
  55. /// </summary>
  56. private decimal maximum;
  57. /// <summary>
  58. /// The minimum value for a Cell
  59. /// </summary>
  60. private decimal minimum;
  61. /// <summary>
  62. /// The alignment of the up and down buttons in the Column
  63. /// </summary>
  64. private LeftRightAlignment upDownAlignment;
  65. /// <summary>
  66. /// Specifies whether the up and down buttons should be drawn
  67. /// </summary>
  68. private bool showUpDownButtons;
  69. #endregion
  70. #region Constructor
  71. /// <summary>
  72. /// Creates a new NumberColumn with default values
  73. /// </summary>
  74. public NumberColumn() : base()
  75. {
  76. this.Init();
  77. }
  78. /// <summary>
  79. /// Creates a new NumberColumn with the specified header text
  80. /// </summary>
  81. /// <param name="text">The text displayed in the column's header</param>
  82. public NumberColumn(string text) : base(text)
  83. {
  84. this.Init();
  85. }
  86. /// <summary>
  87. /// Creates a new NumberColumn with the specified header text and width
  88. /// </summary>
  89. /// <param name="text">The text displayed in the column's header</param>
  90. /// <param name="width">The column's width</param>
  91. public NumberColumn(string text, int width) : base(text, width)
  92. {
  93. this.Init();
  94. }
  95. /// <summary>
  96. /// Creates a new NumberColumn with the specified header text, width and visibility
  97. /// </summary>
  98. /// <param name="text">The text displayed in the column's header</param>
  99. /// <param name="width">The column's width</param>
  100. /// <param name="visible">Specifies whether the column is visible</param>
  101. public NumberColumn(string text, int width, bool visible) : base(text, width, visible)
  102. {
  103. this.Init();
  104. }
  105. /// <summary>
  106. /// Creates a new TextColumn with the specified header text and image
  107. /// </summary>
  108. /// <param name="text">The text displayed in the column's header</param>
  109. /// <param name="image">The image displayed on the column's header</param>
  110. public NumberColumn(string text, Image image) : base(text, image)
  111. {
  112. this.Init();
  113. }
  114. /// <summary>
  115. /// Creates a new TextColumn with the specified header text, image and width
  116. /// </summary>
  117. /// <param name="text">The text displayed in the column's header</param>
  118. /// <param name="image">The image displayed on the column's header</param>
  119. /// <param name="width">The column's width</param>
  120. public NumberColumn(string text, Image image, int width) : base(text, image, width)
  121. {
  122. this.Init();
  123. }
  124. /// <summary>
  125. /// Creates a new TextColumn with the specified header text, image, width
  126. /// and visibility
  127. /// </summary>
  128. /// <param name="text">The text displayed in the column's header</param>
  129. /// <param name="image">The image displayed on the column's header</param>
  130. /// <param name="width">The column's width</param>
  131. /// <param name="visible">Specifies whether the column is visible</param>
  132. public NumberColumn(string text, Image image, int width, bool visible) : base(text, image, width, visible)
  133. {
  134. this.Init();
  135. }
  136. /// <summary>
  137. /// Initializes the NumberColumn with default values
  138. /// </summary>
  139. private void Init()
  140. {
  141. this.Format = "G";
  142. this.maximum = (decimal) 100;
  143. this.minimum = (decimal) 0;
  144. this.increment = (decimal) 1;
  145. this.showUpDownButtons = false;
  146. this.upDownAlignment = LeftRightAlignment.Right;
  147. }
  148. #endregion
  149. #region Methods
  150. /// <summary>
  151. /// Gets a string that specifies the name of the Column's default CellRenderer
  152. /// </summary>
  153. /// <returns>A string that specifies the name of the Column's default
  154. /// CellRenderer</returns>
  155. public override string GetDefaultRendererName()
  156. {
  157. return "NUMBER";
  158. }
  159. /// <summary>
  160. /// Gets the Column's default CellRenderer
  161. /// </summary>
  162. /// <returns>The Column's default CellRenderer</returns>
  163. public override ICellRenderer CreateDefaultRenderer()
  164. {
  165. return new NumberCellRenderer();
  166. }
  167. /// <summary>
  168. /// Gets a string that specifies the name of the Column's default CellEditor
  169. /// </summary>
  170. /// <returns>A string that specifies the name of the Column's default
  171. /// CellEditor</returns>
  172. public override string GetDefaultEditorName()
  173. {
  174. return "NUMBER";
  175. }
  176. /// <summary>
  177. /// Gets the Column's default CellEditor
  178. /// </summary>
  179. /// <returns>The Column's default CellEditor</returns>
  180. public override ICellEditor CreateDefaultEditor()
  181. {
  182. return new NumberCellEditor();
  183. }
  184. #endregion
  185. #region Properties
  186. /// <summary>
  187. /// Gets or sets the maximum value for Column's Cells
  188. /// </summary>
  189. [Category("Appearance"),
  190. Description("The maximum value for Column's Cells")]
  191. public decimal Maximum
  192. {
  193. get
  194. {
  195. return this.maximum;
  196. }
  197. set
  198. {
  199. this.maximum = value;
  200. if (this.minimum > this.maximum)
  201. {
  202. this.minimum = this.maximum;
  203. }
  204. this.OnPropertyChanged(new ColumnEventArgs(this, ColumnEventType.RendererChanged, null));
  205. }
  206. }
  207. /// <summary>
  208. /// Specifies whether the Maximum property should be serialized at
  209. /// design time
  210. /// </summary>
  211. /// <returns>true if the Maximum property should be serialized,
  212. /// false otherwise</returns>
  213. private bool ShouldSerializeMaximum()
  214. {
  215. return this.maximum != (decimal) 100;
  216. }
  217. /// <summary>
  218. /// Gets or sets the minimum value for Column's Cells
  219. /// </summary>
  220. [Category("Appearance"),
  221. Description("The minimum value for Column's Cells")]
  222. public decimal Minimum
  223. {
  224. get
  225. {
  226. return this.minimum;
  227. }
  228. set
  229. {
  230. this.minimum = value;
  231. if (this.minimum > this.maximum)
  232. {
  233. this.maximum = value;
  234. }
  235. this.OnPropertyChanged(new ColumnEventArgs(this, ColumnEventType.RendererChanged, null));
  236. }
  237. }
  238. /// <summary>
  239. /// Specifies whether the Minimum property should be serialized at
  240. /// design time
  241. /// </summary>
  242. /// <returns>true if the Minimum property should be serialized,
  243. /// false otherwise</returns>
  244. private bool ShouldSerializeMinimum()
  245. {
  246. return this.minimum != (decimal) 0;
  247. }
  248. /// <summary>
  249. /// Gets or sets the value to increment or decrement a Cell when its up or down
  250. /// buttons are clicked
  251. /// </summary>
  252. [Category("Appearance"),
  253. Description("The value to increment or decrement a Cell when its up or down buttons are clicked")]
  254. public decimal Increment
  255. {
  256. get
  257. {
  258. return this.increment;
  259. }
  260. set
  261. {
  262. if (value < new decimal(0))
  263. {
  264. throw new ArgumentException("value must be greater than zero");
  265. }
  266. this.increment = value;
  267. }
  268. }
  269. /// <summary>
  270. /// Specifies whether the Increment property should be serialized at
  271. /// design time
  272. /// </summary>
  273. /// <returns>true if the Increment property should be serialized,
  274. /// false otherwise</returns>
  275. private bool ShouldSerializeIncrement()
  276. {
  277. return this.increment != (decimal) 1;
  278. }
  279. /// <summary>
  280. /// Gets or sets whether the Column's Cells should draw up and down buttons
  281. /// </summary>
  282. [Category("Appearance"),
  283. DefaultValue(false),
  284. Description("Determines whether the Column's Cells draw up and down buttons")]
  285. public bool ShowUpDownButtons
  286. {
  287. get
  288. {
  289. return this.showUpDownButtons;
  290. }
  291. set
  292. {
  293. if (this.showUpDownButtons != value)
  294. {
  295. this.showUpDownButtons = value;
  296. this.OnPropertyChanged(new ColumnEventArgs(this, ColumnEventType.RendererChanged, null));
  297. }
  298. }
  299. }
  300. /// <summary>
  301. /// Gets or sets the alignment of the up and down buttons in the Column
  302. /// </summary>
  303. [Category("Appearance"),
  304. DefaultValue(LeftRightAlignment.Right),
  305. Description("The alignment of the up and down buttons in the Column")]
  306. public LeftRightAlignment UpDownAlign
  307. {
  308. get
  309. {
  310. return this.upDownAlignment;
  311. }
  312. set
  313. {
  314. if (!Enum.IsDefined(typeof(LeftRightAlignment), value))
  315. {
  316. throw new InvalidEnumArgumentException("value", (int) value, typeof(LeftRightAlignment));
  317. }
  318. if (this.upDownAlignment != value)
  319. {
  320. this.upDownAlignment = value;
  321. this.OnPropertyChanged(new ColumnEventArgs(this, ColumnEventType.RendererChanged, null));
  322. }
  323. }
  324. }
  325. /// <summary>
  326. /// Gets or sets the string that specifies how a Column's Cell contents
  327. /// are formatted
  328. /// </summary>
  329. [Category("Appearance"),
  330. DefaultValue("G"),
  331. Description("A string that specifies how a column's cell contents are formatted.")]
  332. public new string Format
  333. {
  334. get
  335. {
  336. return base.Format;
  337. }
  338. set
  339. {
  340. base.Format = value;
  341. }
  342. }
  343. /// <summary>
  344. /// Gets the Type of the Comparer used to compare the Column's Cells when
  345. /// the Column is sorting
  346. /// </summary>
  347. public override Type DefaultComparerType
  348. {
  349. get
  350. {
  351. return typeof(NumberComparer);
  352. }
  353. }
  354. #endregion
  355. }
  356. }