Maura Monville

Maura Monville

  • 1.6k
  • 70
  • 5.4k

Erratic behavior of TextBox "TextChanged" event

Apr 8 2022 1:34 PM

I have inserted in the GUI a TextBox. It must hold a number between 0 and 1 that represents a lower threshold for accepting a structure name guessed

by the Dice coefficient. I have set a default but the user can edit the threshold. Therefore I have to check the number entered is between 0 and 1.

The TextBox is implemented as follows:

private void DiceTol_TextChanged(object sender, TextChangedEventArgs e)
{
    if(DiceThreshold < 0 | DiceThreshold > 1)
    {
        MessageBox.Show("Please, enter a number between 0 and 1", "Warning ", MessageBoxButton.OK, MessageBoxImage.Warning);
        DiceThreshold = DiceThresholdDefault;
        DiceTol.Text = DiceThresholdDefault.ToString();
        emptyEditableStructsListBox();
        return;
    }
}
<TextBox x:Name="DiceTol" Grid.Column="1" HorizontalAlignment="Left" Margin="655,0,0,0" Grid.Row="1" Text="{Binding DiceThreshold , Mode=TwoWay}" VerticalAlignment="Bottom"
      Background="Wheat"  FontFamily="Arial Black" FontWeight="Bold" FontSize="20" Width="100" Height="46" Grid.RowSpan="1" TextChanged="DiceTol_TextChanged" TargetUpdated="DiceTol_TargetUpdated" TextInput="DiceTol_TextInput"/>

I developed the code that checks the newly entered number using three different pre-existent TextBox events., namely, "TextChanged", "TargetUpdated", TextInput".
The only event that is triggered is "TextChanged" but it does not work all the times a new number is entered in the TextBox. During the debugging session
first, I erase the default value and then I type in a new value that is on purpose greater than 1 but the "TextChange" event is not triggered. Sometimes it is triggered when I click on a GUI button. The other two events are never triggered.

Here is the code I developed to check the number entered in the TextBox:

I would appreciate your help in fixing the above-illustrated problem.

Thank you


Answers (1)