Erratic behavior of TextBox "TextChanged" event

mauede

Well-known member
Joined
Sep 1, 2021
Messages
103
Location
Northwood - UK
Programming Experience
Beginner
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:

TextBox TextChanged event:
  <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:

TextChange event handler:
  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;
            }
        }

I would appreciate your help in fixing the above-illustrated problem.
Thank you
 
Not a direct answer for your question, but it seems that your objective is to implement validation. So why not use the existing infrastructure for it:
 
A more accessible article regarding validation in WPF:
 
Not a direct answer for your question, but it seems that your objective is to implement validation. So why not use the existing infrastructure for it:
Sorry. It does not work for me. The property "Source" does not belong to TextBox.
I inserted the TextBox.Text element but still "Source" is not found. If I hover on it I can see a message stating that a namespace is missing.
 
Perhaps I'm missing something. Source is a property of the Binding, not of the TextBox if you look closely at the sample code in that MSDN link.
 
I think I have to get my head around how classes properties are accessed.
If I define the following property in the MainWindow class that is part of namespace "WPFUI" :

DiceThreshold property definition:
 private double _diceThreshold = 0.33;
        public double DiceThresholdDefault = 0.33;
        public double DiceThreshold
        {
            get { return _diceThreshold; }
            set
            {
                if (value < 0 || value > 1)
                    throw new ArgumentOutOfRangeException("value");
                _diceThreshold = value;
            }
        }

Then the compiler does not resolve the name "DiceThreshold" used inside the other class "PTAccess" which is part of the same namespace "WPFUI".
I don't get it as "DiceThreshold" is a PUBLIC property.....????
How am I supposed to use the properties defined in MainWindow class from another external class (PTAccess) given that everything belongs to the same namespace?
Shall I create an instance of MainWindow from class PTAceess to get a handle on properties defined inside MainWindow?

I moved the above definition of property "DiceThreshold" inside PTAccess class and accessed its fields from inside MainWindow class by creating a new instance of the class as follows:


Accessing the field of a property from an external class:
PTAccess pt = new PTAccess();
            if (pt.DiceThreshold < 0 | pt.DiceThreshold > 1)

The compiler does not complain but at runtime, I can type inside the textbox whatever number I want and nothing happens.
The TextBox is defined as follows:


extBox definition in MainWindow.xaml:
 <TextBox x:Name="DiceTol" Grid.Column="1" HorizontalAlignment="Left" Margin="655,0,0,0" Grid.Row="1"  VerticalAlignment="Bottom"
                          Background="Wheat"  FontFamily="Arial Black" FontWeight="Bold" FontSize="20" Width="100" Height="46" Grid.RowSpan="1"
                            TextChanged="DiceTol_TextChanged" >
            <TextBox.Text>
                <Binding  Path="DiceThreshold, Mode=TwoWay" UpdateSourceTrigger="PropertyChanged"  ValidatesOnDataErrors="True"  />
            </TextBox.Text>
       </TextBox>

I reckon the MainWindow.XAML controls look for corresponding property names inside the class MainWindow and discard all definitions in any other class that is part of the same solution/program.
I started to appreciate the MVVM scheme that I refused out of lack of familiarity.
 

Latest posts

Back
Top Bottom