Project/02 - Engine/LittleBigTools/UserControls/NumericUpDownTextBox.xaml.cs
/// <summary>
/// Ensures that keypress is a valid character (numeric or negative sign)
/// - negative sign ('-') only allowed if Minimum value less than 0, the entry 
///     is at the beginning of the text and there is not already a negative sign
/// - number only allowed if not beginning of text and there is a negative sign
/// </summary>
/// <param name="sender"></param>
/// <param name="e">Contains arguments associated with changes to
///                          a System.Windows.Input.TextComposition</param>
Ensures that keypress is a valid character (numeric or negative sign)
negative sign ('-') only allowed if Minimum value less than 0, the entry
is at the beginning of the text and there is not already a negative sign
number only allowed if not beginning of text and there is a negative sign
Ensures that keypress is a valid character ( numeric or negative sign ) negative sign ( ' - ' ) only allowed if Minimum value less than 0 the entry is at the beginning of the text and there is not already a negative sign number only allowed if not beginning of text and there is a negative sign
private void control_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if ("0123456789".IndexOf(e.Text) < 0)
            {
                if (e.Text == "-" && MinValue < 0)
                {
                    if (Text.Length == 0 || (CaretIndex == 0 &&
                                    Text[0] != '-'))
                    {
                        e.Handled = false;
                        return;
                    }
                }
                e.Handled = true;
            }
            else 
            {
                if (Text.Length > 0 && CaretIndex == 0 &&
                    Text[0] == '-' && SelectionLength == 0)
                {
                    e.Handled = true;
                }
                else
                {
                    StringBuilder sb = new StringBuilder(Text);
                    sb.Remove(CaretIndex, SelectionLength);
                    sb.Insert(CaretIndex, e.Text);
                    int newValue = int.Parse(sb.ToString());
                    if (FixValueKeyPress(newValue))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                    }
                }
            }
        }
private void control_PreviewTextInput ( object sender , TextCompositionEventArgs e ) { if ( " " . IndexOf ( e . Text ) < 0 ) { if ( e . Text == " " && MinValue < 0 ) { if ( Text . Length == 0 || ( CaretIndex == 0 && Text [ 0 ] != ' ' ) ) { e . Handled = false ; return ; } } e . Handled = true ; } else { if ( Text . Length > 0 && CaretIndex == 0 && Text [ 0 ] == ' ' && SelectionLength == 0 ) { e . Handled = true ; } else { StringBuilder sb = new StringBuilder ( Text ) ; sb . Remove ( CaretIndex , SelectionLength ) ; sb . Insert ( CaretIndex , e . Text ) ; int newValue = int . Parse ( sb . ToString ( ) ) ; if ( FixValueKeyPress ( newValue ) ) { e . Handled = false ; } else { e . Handled = true ; } } } }
Ensures that keypress is a valid character (numeric or negative sign)
 negative sign ('-') only allowed if Minimum value less than 0, the entry
is at the beginning of the text and there is not already a negative sign
 number only allowed if not beginning of text and there is a negative sign
Ensures that keypress is a valid character ( numeric or negative sign ) negative sign ( ' - ' ) only allowed if Minimum value less than 0 the entry is at the beginning of the text and there is not already a negative sign number only allowed if not beginning of text and there is a negative sign
// Catch any non-numeric keys // else check for negative sign // A digit has been pressed // We now know that have good value: check for attempting to put number before '-' // Can't put number before '-' // check for what new value will be: // check if beyond allowed values
sender
object
None
e
TextCompositionEventArgs
Contains arguments associated with changes to
a System.Windows.Input.TextComposition
Contains arguments associated with changes to a System . Windows . Input . TextComposition