How does "LostFocus" work

mauede

Well-known member
Joined
Sep 1, 2021
Messages
103
Location
Northwood - UK
Programming Experience
Beginner
In the WPF application, I am trying to develop, I have a TextBox where the user enters the identifier of the patient whose data are to be extracted and displayed.
I am using the event "Property_Changed", available by default, to react to the user erasing the current patient_Id and entering another one. My code cleans all the controls displaying the previous patient data to make them ready to display the new patient data.
If I place a break-point in the code block that gets the control upon changing the content of the TextBox
I see the break-point is triggered for every single character entered in the TextBox. That is not the ideal behaviour.
Ideally, I would like that code block to be run only when the user has completed entering the patient_Id.
I am thinking of using the LostFocus event if it is in the TextBox event list available from Visual Studio.
Shall I expect the code block to be run only when the user mouse leaves the TextBox pointing outside it or when the user clicks anywhere on the GUI outside the TextBox?
Either way, the code block will have to check if the patient_Id has really been changed before wiping out all the displayed patient data. The user might click inside TextBox.Text and then click outside it without changing anything.
At the time being my code does not do that. That would require to get access to TextBox.Text content when TextBox loses focus. Does this need to be an "OneWayToSource" binding type?
At the moment the C# code does not care about the content of TextBox.Text.
I would appreciate some guidelines on how to deal with LostFocus if this is what I need.

I just found out that none of the predisposed events "LostFocus", "TextInput",
"FocusableChanged" works for TextBox. I duplicated the code to respond to each one of the above-mentioned events and placed a breakpoint at the beginning of
the code block. None of these events has been triggered when typing a string in the TextBox.
 
Last edited:
Back
Top Bottom