简体   繁体   中英

How to restrict user to TextBox if value entered is incorrect

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?

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 :)

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM