0

In my program I have a TextBox who's value must be set in a specific integer range. If it is not within this range, it should warn the user and then highlight the incorrect text inside of the TextBox for re-editing (implying that the user must enter a value that is in the correct range before they are allowed to leave the TextBox). How would I change my code so that it performs these operations?

This is what I have so far. I am using a TextChanged event. This code warns the user about the restriction breach and refocuses (I would like to highlight the value instead) on the TextBox, but does not prevent the user from clicking out of it afterward:

int maxRevSpeed;

//Max Rev Speed -- Text Changed
private void maxRevSpeed_textChanged(object sender, RoutedEventArgs e)
{
    if (maxRevSpeed_textBox.Text == "" || maxRevSpeed_textBox.Text == " ")
        maxRevSpeed = 0;
    else
    {
        maxRevSpeed = Convert.ToInt32(maxRevSpeed_textBox.Text);

        if (maxRevSpeed <= 0 || maxRevSpeed > 45)
        {
            MessageBox.Show("Reverse Sensor speed must be between 0 and 45 FPM", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
        }

        maxRevSpeed_textBox.Focus();
    }
}

Please note that this question is a revisit of a former question of mine. I am aware that it may be "frowned upon" to take this approach to a TextBox, but regardless I would still like to figure out how to implement such a thing. Thank you.

Update 1:

After looking at everyone's suggestions I have updated my code:

//Max Rev Speed -- Text Changed
private void maxRevSpeed_textChanged(object sender, RoutedEventArgs e)
{
    if (maxRevSpeed_textBox.Text == "" || maxRevSpeed_textBox.Text == " ") //Is Empty or contains spaces
        maxRevSpeed = 0;
    else if (!Regex.IsMatch(maxRevSpeed_textBox.Text, @"^[\p{N}]+$")) //Contains characters
        maxRevSpeed = 0;
    else
        maxRevSpeed = Convert.ToInt32(maxRevSpeed_textBox.Text);
}

//Max Rev Speed -- Lost Focus
private void maxRevSpeed_LostFocus(object sender, RoutedEventArgs e)
{
    if (maxRevSpeed <= 0 || maxRevSpeed > 45)
    {
        MessageBox.Show("Reverse Sensor speed must be between 0 and 45 FPM", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);

        //Supposed to highlight incorrect text -- DOES NOT WORK
        maxRevSpeed_textBox.SelectionStart = 0;
        maxRevSpeed_textBox.SelectionLength = maxRevSpeed_textBox.Text.Length;
    }
}

The integer representing the text in the textBox is now dealt with in the textChanged event. The LostFocus event handles the warning and the re-selection of the incorrect text value. However, the highlight text method works when it is in the textChanged event, but not in it's current location. Why is that, and how can I fix it?

Community
  • 1
  • 1
Eric after dark
  • 1,768
  • 4
  • 31
  • 79
  • have you thought about changing the if condition to check for `if(IsNullOrEmpty(maxRevSpeed_textBox.Text) || maxRevSpeed_textBox.Text.Contains(" ")` – MethodMan May 22 '14 at 16:02
  • I don't understand... you want to focus the `TextBox` that the user is currently typing into... and therefore already has focus? – Sheridan May 22 '14 at 16:02
  • Also, you'll need some error checking on `maxRevSpeed = Convert.ToInt32(maxRevSpeed_textBox.Text);` – Sheridan May 22 '14 at 16:03
  • also look up how to use `Char.IsWhiteSpace` function – MethodMan May 22 '14 at 16:03
  • @DJKRAZE I tried this and it says that `The name 'IsNullOrEmpty' does not exist in the current context`. I have `System` declared in the .cs file... – Eric after dark May 22 '14 at 17:23
  • @Sheridan I added some more error checking on the line that you specified. To clarify, I would like to make it so that the user can't leave the `textBox` until a valid value is entered. – Eric after dark May 22 '14 at 18:06

3 Answers3

1

You can prevent the user from entering the text or out of range by using PreviewTextInput handler of textbox, call it like this.

private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!char.IsDigit(e.Text, e.Text.Length - 1))
            {
                e.Handled = true;
            }
        }

The code above is for entering numbers only, you can change it according to your requirements, hope it helps :)

Saad Abdullah
  • 2,252
  • 3
  • 29
  • 42
1

If you just want to stop focus from leaving a TextBox, all you need to do is to set the Handled property of the KeyboardFocusChangedEventArgs object to true in a PreviewLostKeyboardFocus handler when your invalid condition is true:

private void PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    e.Handled = IsInvalidValue;
}

This of course assumes that you have a property named IsInvalidValue that you set to true when the entered data is invalid and false otherwise.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
0

Hi I suppose you are using C#, here you can find a relevant post: C# auto highlight text in a textbox control

As they stated the following code should select the text inside texbox

In Windows Forms and WPF:

maxRevSpeed_textBox.SelectionStart = 0; maxRevSpeed_textBox.SelectionLength = textbox.Text.Length;

Community
  • 1
  • 1
Memin
  • 3,788
  • 30
  • 31
  • This works in the `TextChanged` event, but not in the `LostFocus` event, which I think would be the prime spot to put it. Why doesn't it work here? – Eric after dark May 22 '14 at 17:37