/// <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 overriden 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="Control.LostFocus" /> protected override void HandleLostFocus(object sender, EventArgs e) { TraceLine("NumericBehavior.HandleLostFocus"); if (!HasFlag((int)LostFocusFlag.Max)) return; string originalText = GetNumericText(m_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] == m_negativeSign); if (isNegative) text = text.Substring(1); text = text.TrimStart('0'); if (text == "" || text[0] == m_decimalPoint) text = '0' + text; if (isNegative) text = m_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 = m_maxDecimalPlaces; int maxWholeDigits = m_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 && m_decimalPoint != '.') text = text.Replace('.', m_decimalPoint); // remember the current selection using (Selection.Saver savedSelection = new Selection.Saver(m_textBox)) { m_textBox.Text = text; } } }
/// <summary> /// Sets the AM or PM symbol if not in 24-hour format. </summary> /// <param name="am"> /// If true, sets the AM symbol; otherwise it sets the PM symbol. </param> /// <seealso cref="AMPM" /> public void SetAMPM(bool am) { if (Show24HourFormat) return; using (Selection.Saver savedSelection = new Selection.Saver(m_textBox)) // remember the current selection { m_selection.Set(GetAMPMStartPosition() - 1, GetAMPMStartPosition() + m_ampmLength); m_selection.Replace(" " + (am ? m_am : m_pm)); // set the AM/PM } }
/// <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 overriden 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="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(m_textBox); // save the selection before the text changes bool textChangedByKeystroke = m_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 (m_previousSeparatorCount >= 0) { using (savedSelection) { int newSeparatorCount = GetGroupSeparatorCount(m_textBox.Text); if (m_previousSeparatorCount != newSeparatorCount && savedSelection.Start > m_prefix.Length) savedSelection.MoveBy(newSeparatorCount - m_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); m_textChangedByKeystroke = false; }