SpinEdit.cs
Upload User: linjidu
Upload Date: 2021-04-29
Package Size: 49798k
Code Size: 12k
Category:

CSharp

Development Platform:

C#

  1. #region Copyright (c) 2000-2009 Developer Express Inc.
  2. /*
  3. {*******************************************************************}
  4. {                                                                   }
  5. {       Developer Express .NET Component Library                    }
  6. {       XtraEditors                                 }
  7. {                                                                   }
  8. {       Copyright (c) 2000-2009 Developer Express Inc.              }
  9. {       ALL RIGHTS RESERVED                                         }
  10. {                                                                   }
  11. {   The entire contents of this file is protected by U.S. and       }
  12. {   International Copyright Laws. Unauthorized reproduction,        }
  13. {   reverse-engineering, and distribution of all or any portion of  }
  14. {   the code contained in this file is strictly prohibited and may  }
  15. {   result in severe civil and criminal penalties and will be       }
  16. {   prosecuted to the maximum extent possible under the law.        }
  17. {                                                                   }
  18. {   RESTRICTIONS                                                    }
  19. {                                                                   }
  20. {   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
  21. {   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
  22. {   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
  23. {   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
  24. {   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
  25. {                                                                   }
  26. {   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
  27. {   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
  28. {   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
  29. {   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
  30. {   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
  31. {                                                                   }
  32. {   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
  33. {   ADDITIONAL RESTRICTIONS.                                        }
  34. {                                                                   }
  35. {*******************************************************************}
  36. */
  37. #endregion Copyright (c) 2000-2009 Developer Express Inc.
  38. using System;
  39. using System.Drawing;
  40. using System.Collections;
  41. using System.ComponentModel;
  42. using System.Globalization;
  43. using System.Windows.Forms;
  44. using DevExpress.LookAndFeel;
  45. using DevExpress.Utils;
  46. using DevExpress.Utils.Design;
  47. using DevExpress.Utils.Drawing;
  48. using DevExpress.XtraEditors;
  49. using DevExpress.XtraEditors.ViewInfo;
  50. using DevExpress.XtraEditors.Repository;
  51. using DevExpress.XtraEditors.Controls;
  52. using DevExpress.XtraEditors.Drawing;
  53. using DevExpress.XtraEditors.Mask;
  54. namespace DevExpress.XtraEditors.Repository {
  55. public class RepositoryItemSpinEdit : RepositoryItemBaseSpinEdit {
  56. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never), Browsable(false), Obsolete(ObsoleteText.SRObsoletePropertiesText)]
  57. public new RepositoryItemSpinEdit Properties { get { return this; } }
  58. private static object valueChanged = new object();
  59. decimal _minValue, _maxValue, _increment;
  60. bool _isFloatValue;
  61. public RepositoryItemSpinEdit() {
  62. this._isFloatValue = true;
  63. this._minValue = this._maxValue = Decimal.Zero;
  64. this._increment = 1;
  65. }
  66. protected override MaskProperties CreateMaskProperties() {
  67. return new NumericMaskProperties();
  68. }
  69. public override void Assign(RepositoryItem item) {
  70. RepositoryItemSpinEdit source = item as RepositoryItemSpinEdit;
  71. BeginUpdate();
  72. try {
  73. base.Assign(item);
  74. if(source == null) return;
  75. this._increment = source.Increment;
  76. this._isFloatValue = source.IsFloatValue;
  77. this._maxValue = source.MaxValue;
  78. this._minValue = source.MinValue;
  79. } finally {
  80. EndUpdate();
  81. }
  82. Events.AddHandler(valueChanged, source.Events[valueChanged]);
  83. }
  84. [Browsable(false)]
  85. public override string EditorTypeName { get { return "SpinEdit"; } }
  86. protected new SpinEdit OwnerEdit { get { return base.OwnerEdit as SpinEdit; } }
  87. bool ShouldSerializeMinValue() { return MinValue != Decimal.Zero; }
  88. [Category(CategoryName.Behavior), Description("Gets or sets the editor's minimum value."), RefreshProperties(System.ComponentModel.RefreshProperties.All)]
  89. public decimal MinValue {
  90. get { return _minValue; }
  91. set {
  92. if(MinValue == value) return;
  93. _minValue = value;
  94. if(MaxValue < MinValue) MaxValue = MinValue;
  95. OnMinMaxChanged();
  96. OnPropertiesChanged();
  97. }
  98. }
  99. bool ShouldSerializeMaxValue() { return MaxValue != Decimal.Zero; }
  100. [Category(CategoryName.Behavior), Description("Gets or sets the editor's maximum value."), RefreshProperties(System.ComponentModel.RefreshProperties.All)]
  101. public decimal MaxValue {
  102. get { return _maxValue; }
  103. set {
  104. if(MaxValue == value) return;
  105. _maxValue = value;
  106. if(MinValue > MaxValue) MinValue = MaxValue;
  107. OnMinMaxChanged();
  108. OnPropertiesChanged();
  109. }
  110. }
  111. bool ShouldSerializeIncrement() { return Increment != 1; }
  112. [Category(CategoryName.Behavior), Description("Gets or sets a value by which the editor's value changes each time the keyboard UP or DOWN key is pressed or the spin button is clicked.")]
  113. public decimal Increment {
  114. get { return _increment; }
  115. set {
  116. if(Increment == value) return;
  117. _increment = value;
  118. OnPropertiesChanged();
  119. }
  120. }
  121. [Category(CategoryName.Behavior), Description("Gets or sets a value specifying whether the editor's value is a float or integer."), DefaultValue(true)]
  122. [RefreshProperties(RefreshProperties.All)]
  123. public bool IsFloatValue {
  124. get { return _isFloatValue; }
  125. set {
  126. if(IsFloatValue == value) return;
  127. _isFloatValue = value;
  128. OnPropertiesChanged();
  129. if(!IsLoading) {
  130. if(IsFloatValue) {
  131. if(Mask.EditMask == "N00")
  132. Mask.EditMask = string.Empty;
  133. } else {
  134. if(Mask.EditMask == string.Empty)
  135. Mask.EditMask = "N00";
  136. }
  137. }
  138. }
  139. }
  140. protected virtual void OnMinMaxChanged() {
  141. if(OwnerEdit != null) OwnerEdit.OnMinMaxChanged();
  142. }
  143. protected internal decimal CorrectValue(decimal val) {
  144. if(!this.IsFloatValue)
  145. val = Math.Round(val);
  146. if(this.MaxValue != Decimal.Zero || this.MinValue != Decimal.Zero) {
  147. if(val < this.MinValue)
  148. val = this.MinValue;
  149. if(val > this.MaxValue)
  150. val = this.MaxValue;
  151. }
  152. return val;
  153. }
  154. protected internal override void RaiseEditValueChanged(EventArgs e) {
  155. if(OwnerEdit != null && !IsNullValue(OwnerEdit.EditValue)) {
  156. decimal currentValue = OwnerEdit.Value;
  157. decimal checkedValue = CorrectValue(currentValue);
  158. if(currentValue != checkedValue)
  159. return;
  160. }
  161. base.RaiseEditValueChanged(e);
  162. }
  163. public override bool IsNeededKey(Keys keyData) {
  164. if(OwnerEdit != null && OwnerEdit.InplaceType == InplaceType.Bars && (keyData == Keys.Up || keyData == Keys.Down))
  165. return true;
  166. return base.IsNeededKey(keyData);
  167. }
  168. protected override bool AllowFormatEditValue { get { return false; } }
  169. protected override bool AllowParseEditValue { get { return true; } }
  170. [Browsable(false)]
  171. public override HorzAlignment DefaultAlignment { get { return HorzAlignment.Far; } }
  172. [Description("Gets or sets an edit mask."), Category(CategoryName.Format),
  173. DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
  174. RefreshProperties(RefreshProperties.All)
  175. ]
  176. public string EditMask {
  177. get { return Mask.EditMask; }
  178. set { Mask.EditMask = value; }
  179. }
  180. [Description("Occurs after the value of the SpinEdit.Value property has been changed."), Category(CategoryName.Events)]
  181. public event EventHandler ValueChanged {
  182. add { this.Events.AddHandler(valueChanged, value); }
  183. remove { this.Events.RemoveHandler(valueChanged, value); }
  184. }
  185. protected override void RaiseEditValueChangedCore(EventArgs e) {
  186. base.RaiseEditValueChangedCore(e);
  187. RaiseValueChanged(e);
  188. }
  189. protected internal virtual void RaiseValueChanged(EventArgs e) {
  190. EventHandler handler = (EventHandler)this.Events[valueChanged];
  191. if(handler != null) handler(GetEventSender(), e);
  192. }
  193. }
  194. }
  195. namespace DevExpress.XtraEditors {
  196. [ToolboxItem(true), DefaultBindingPropertyEx("Value"),
  197.  ToolboxBitmap(typeof(DevExpress.XtraEditors.BaseEdit), DevExpress.Utils.ControlConstants.BitmapPath + "SpinEdit.bmp"),
  198.  Description("Allows an end-user to edit numeric values and provides up and down buttons for incrementing and decrementing values."),
  199.  ToolboxTabName(AssemblyInfo.DXTabCommon)
  200. ]
  201. public class SpinEdit : BaseSpinEdit {
  202. public SpinEdit() {
  203. this.fEditValue = Decimal.Zero;
  204. }
  205. [Browsable(false)]
  206. public override string EditorTypeName { get { return "SpinEdit"; } }
  207. [Description("Gets an object containing properties, methods and events specific to the spin edit control."), Category(CategoryName.Properties), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  208. public new RepositoryItemSpinEdit Properties { get { return base.Properties as RepositoryItemSpinEdit; } }
  209. [Bindable(false), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  210. public override string Text {
  211. get { return base.Text; }
  212. set { base.Text = value; }
  213. }
  214. decimal ConvertToDecimal(object val) {
  215. if(val == null)
  216. return Decimal.Zero;
  217. decimal res = Decimal.Zero;
  218. try { res = Convert.ToDecimal(val); } catch { }
  219. return res;
  220. }
  221. object ConvertFromDecimal(decimal val) {
  222. if(!Properties.IsFloatValue) {
  223. try {
  224. val = Math.Round(val);
  225. return (Int32)val;
  226. } catch { }
  227. try { return (UInt32)val; } catch { }
  228. try { return (Int64)val; } catch { }
  229. try { return (UInt64)val; } catch { }
  230. }
  231. return val;
  232. }
  233. [Bindable(ControlConstants.NonObjectBindable), Description("Gets or sets a spin editor's value."), Category(CategoryName.Appearance), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  234. public virtual decimal Value {
  235. get {
  236. FlushPendingEditActions();
  237. return ConvertToDecimal(EditValue);
  238. }
  239. set {
  240. EditValue = ConvertFromDecimal(value);
  241. }
  242. }
  243. [Browsable(false)]
  244. public override object EditValue {
  245. get {
  246. return base.EditValue;
  247. }
  248. set {
  249. if(Properties.IsNullValue(value))
  250. value = null;
  251. else
  252. value = ConvertToDecimal(value);
  253. base.EditValue = value;
  254. }
  255. }
  256. protected override object ExtractParsedValue(ConvertEditValueEventArgs e) {
  257. if(e.Handled)
  258. return e.Value;
  259. if(Properties.IsNullInputAllowed && Properties.IsNullValue(e.Value))
  260. return null;
  261. return Properties.CorrectValue(ConvertToDecimal(e.Value));
  262. }
  263. protected internal virtual void OnMinMaxChanged() {
  264. if(IsLoading) return;
  265. if(Properties.IsNullInputAllowed && Properties.IsNullValue(this.EditValue)) {
  266. } else {
  267. Value = Properties.CorrectValue(Value);
  268. }
  269. }
  270. decimal Increment(decimal val, decimal inc) {
  271. if(Properties.ReadOnly) return val;
  272. try {
  273. return Properties.CorrectValue(val + inc);
  274. } catch {
  275. return inc > Decimal.Zero ? Properties.MaxValue : Properties.MinValue;
  276. }
  277. }
  278. protected override void OnSpin(SpinEventArgs e) {
  279. if(Properties.ReadOnly) return;
  280. if(Properties.Increment == Decimal.Zero) {
  281. base.OnSpin(e);
  282. } else {
  283. Properties.RaiseSpin(e);
  284. if(e.Handled)
  285. return;
  286. decimal delta = e.IsSpinUp ? Properties.Increment : -Properties.Increment;
  287. Value = Increment(Value, delta);
  288. e.Handled = true;
  289. }
  290. }
  291. [Description("Occurs after the value of the SpinEdit.Value property has been changed."), Category(CategoryName.Events)]
  292. public event EventHandler ValueChanged {
  293. add { Properties.ValueChanged += value; }
  294. remove { Properties.ValueChanged -= value; }
  295. }
  296. }
  297. }