Ejemplo n.º 1
0
		/// <summary>Handles when the control has lost its focus. </summary>
		/// <param name="sender">The object who sent the event. </param>
		/// <param name="e">The event data. </param>
		/// <remarks>
		/// This method is overridden from the Behavior class and it  
		/// handles the textbox's LostFocus event. 
		/// Here it checks the value's against the allowed range and adds any missing zeros. </remarks>
		/// <seealso cref="WinForms.Control.LostFocus" />
		protected override void HandleLostFocus(object sender, EventArgs e)
		{
			TraceLine("NumericBehavior.HandleLostFocus");

			if (!HasFlag((int)LostFocusFlag.Max))
				return;

			string originalText = GetNumericText(this.textBox.Text, true);
			string text = originalText;
			int length = text.Length;

			// If desired, remove any extra leading zeros but always leave one in front of the decimal point
			if (HasFlag((int)LostFocusFlag.RemoveExtraLeadingZeros) && length > 0)
			{
				bool isNegative = (text[0] == this.negativeSign);
				if (isNegative)
					text = text.Substring(1);
				text = text.TrimStart('0');
				if (text == "" || text[0] == this.decimalPoint)
					text = '0' + text;
				if (isNegative)
					text = this.negativeSign + text;
			}
			// Check if the value is empty and we don't want to touch it
			else if (length == 0 && HasFlag((int)LostFocusFlag.DontPadWithZerosIfEmpty))
				return;

			int decimalPos = text.IndexOf('.');
			int maxDecimalPlaces = this.maxDecimalPlaces;
			int maxWholeDigits = this.maxWholeDigits;

			// Check if we need to pad the number with zeros after the decimal point
			if (HasFlag((int)LostFocusFlag.PadWithZerosAfterDecimal) && maxDecimalPlaces > 0)
			{
				if (decimalPos < 0)
				{
					if (length == 0 || text == "-")
					{
						text = "0";
						length = 1;
					}
					text += '.';
					decimalPos = length++;
				}

				text = InsertZeros(text, -1, maxDecimalPlaces - (length - decimalPos - 1));
			}

			// Check if we need to pad the number with zeros before the decimal point
			if (HasFlag((int)LostFocusFlag.PadWithZerosBeforeDecimal) && maxWholeDigits > 0)
			{
				if (decimalPos < 0)
					decimalPos = length;

				if (length > 0 && text[0] == '-')
					decimalPos--;

				text = InsertZeros(text, (length > 0 ? (text[0] == '-' ? 1 : 0) : -1), maxWholeDigits - decimalPos);
			}

			if (text != originalText)
			{
				if (decimalPos >= 0 && this.decimalPoint != '.')
					text = text.Replace('.', this.decimalPoint);

				// remember the current selection 
				using (Selection.Saver savedSelection = new Selection.Saver(this.textBox))
				{
					this.textBox.Text = text;
				}
			}
		}
Ejemplo n.º 2
0
		/// <summary>Handles changes in the textbox text. </summary>
		/// <param name="sender">The object who sent the event. </param>
		/// <param name="e">The event data. </param>
		/// <remarks>
		/// This method is overridden from the Behavior class and it  
		/// handles the textbox's TextChanged event. 
		/// Here it is used to adjust the selection if new separators have been added or removed. </remarks>
		/// <seealso cref="WinForms.Control.TextChanged" />
		// Fires the TextChanged event if the text is valid.
		protected override void HandleTextChanged(object sender, EventArgs e)
		{
			TraceLine("NumericBehavior.HandleTextChanged");

			Selection.Saver savedSelection = new Selection.Saver(this.textBox);  // save the selection before the text changes
			bool textChangedByKeystroke = this.textChangedByKeystroke;
			base.HandleTextChanged(sender, e);

			// Check if the user has changed the number enough to cause
			// one or more separators to be added/removed, in which case
			// the selection may need to be adjusted.
			if (this.previousSeparatorCount >= 0)
			{
				using (savedSelection)
				{
					int newSeparatorCount = GetGroupSeparatorCount(this.textBox.Text);
					if (this.previousSeparatorCount != newSeparatorCount && savedSelection.Start > this.prefix.Length)
						savedSelection.MoveBy(newSeparatorCount - this.previousSeparatorCount);
				}
			}

			// If the text wasn't changed by a keystroke and the UseLostFocusFlagsWhenTextPropertyIsSet flag is set,
			// call the LostFocus handler to adjust the value according to whatever LostFocus flags are set.
			if (HasFlag((int)LostFocusFlag.CallHandlerWhenTextChanges) ||
			   (!textChangedByKeystroke && HasFlag((int)LostFocusFlag.CallHandlerWhenTextPropertyIsSet)))
				HandleLostFocus(sender, e);

			this.textChangedByKeystroke = false;
		}