コード例 #1
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 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;
        }