What Textbox event is raised right after bound data source is updated? - c#

In windows forms, when I tab out of a text box, the bound data source value is updated. I'd like to capture the events right before and right after the data source changed. I think the OnLeave event is what I want for the before event. In the debugger, I'm not seeing the data source value changed. But, what event can I key off of for the after event?

I don't think there is an event that does exactly what you're asking, the closest I think you're going to get is to use the DataBindings and find your specific Binding and the you can catch the Parse event. But I believe this event fires before the data is pushed back to the source, so it's not much better than the LostFocus event.

The default event for TextBox DataBindings is DataSourceUpdateMode.OnValidation. When you tab out of the TextBox, the following events will fire:
Leave
Validating
(data source gets updated)
Validated
The Validating event has a CancelEventArgs parameter that allows you to cancel the leave attempt for the TextBox (the focus will remain in the TextBox).
If you use DataSourceUpdateMode.OnPropertyChanged, it will update the data source with every keystroke or text change.

Related

WPF RaisePropertyChanged event on lost focus

I have a C# WPF MVVM application that works fine.
The only problem is when I modify a textbox and click on the menu. If I do that without clicking on another control, the view->viewmodel event is never fired because the textbox hasn't lost focus. Correct me if I am wrong, but I think the RaisePropertyChanged is only fired on LostFocus (or OnBlur, or any similar event).
So, clicking on the menu save button right after editing the textbox causes the viewmodel to save the data using old values.
So, resuming:
This sequence works fine:
Edit the text box
Click on another control
RaisePropertyChanged is fired, the viewmodel is updated
Click on save button on the menu
Data Saved with correct values
This sequence gives me an error:
Edit the text box
Click on save button on the menu
Data Saved with correct values
How to solve this?
This is a common gotcha with TextBoxes in both WPF and WinForms. You can get around this by instructing the binding system to update the VM with every change to the TextBox instead of when it loses focus. To do this, set the UpdateSourceTrigger of the binding to PropertyChanged. This will write back to the VM any time the TextBox raises the PropertyChanged event for its Text property.
<TextBox Text="{Binding MyText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
For the TextBox.Text dependency property, its default UpdateSourceTrigger is LostFocus (ie, your view model property gets updated when the control loses focus). To make the property update immediately whenever text is entered, set UpdateSourceTrigger=PropertyChanged. (See the link above for more info -- it actually covers your example specifically.)

How to raise a textBox_InsertPlaceChanged event in TextBox at C#?

I couldnt make good question i know,but it is what i could.When user write something in textbox there appear symbol flashing which shows place which will be inserted character.It can be changed by left and right arrow buttons.I want to raise event when it is changed or something that i can get index of this "symbol".is there any property at textBox for it?
The "symbol" is called a caret. When it's position changes, the SelectionChanged event is fired, and you can programmatically retrieve the position using the CaretIndex property on the TextBox.
You can find more information about this here.

BindingSource.EndEdit() doesn't write values to datasource

I had hoped that the EndEdit() method would take all bindings and write the control values values back to the datasource. Suprisingly, it doesn't always work. Sometimes the values are not written back to the datasource.
When I want to ensure that the controls write their data back to the datasource, is my only option to get each control's databinding and call WriteValue()?
Any other thoughts or ideas?
UPDATE
I bind to an object.
I have a textbox and I handle the TextChanged event. Within this handler I call EndEdit().
When I bind to the datasource, the TextChanged event is called which triggers the EndEdit(). If I then type into the textbox, the FIRST call to EndEdit() has no effect. After that it seems to work.
Note: I can get this to work if I do not have a TextChanged handler until AFTER the data is bound to the TextBox. It seems that the first EndEdit call (during the binding) seems to have a bad effect.
EndEdit() only works if binding source is in editing state, which is usually triggered by entering into control or starting actual editing.

TextBox TextChanged event does not fire when Visible = False?

I have a textbox bound to a datasource. The textbox's TextChanged event updates another textbox.
The problem is, I do not want the first textbox to show, so I set its Visible property to false.
However, now the TextChanged event does not fire!
I can work around it by setting Visible=True, Left=-100000 on form load, but I'd like a proper solution.
Can anyone offer an explanation?
Set your textbox.Visible = false in the FormLoad event instead of in the designer. It has to do with handle creation. If the textbox is not visible during construction, then the handle is not created. If the textbox is made invisible after construction, then the handle will have been created and events will occur.
See this discussion on MSDN.
An alternative solution to the accepted answer is to set up the TextChanged listener on Loaded, this works for me just the same (in Silverlight at least) and keeps the designer view as it should be.
What type of datasource is it? It might have an event that you can use directly instead of using a textbox to listen for an update.
If Visible is equal to false then the Control is not rendered. Therefore it will be unable to fire an event.
Instead, set the style to display:none. You can set/unset this programmatically using the Attributes collection:
MyTextBox.Attributes.Add("style", "display: none");

Detecting changes to the source data using the XmlDataProvider and a bound TextBox

I have a TreeView bound to an XmlDataProvider. The name of a node is reflected in the tree and the data of the node is in a TextBox. When you click on a tree item, it loads the data in the TextBox.
What is the best way to determine when a user has changed the source data referenced by the XmlDataProvider.
I would like to put an asterisk (*) next to the filename to say that the document is dirty and should be saved. This will also determine whether the user is reminded to save when the exit the program.
Unfortunately, when the TextBox is updated by the two-way binding, it fires the TextChanged event.
Is there a way to determine that the TextChanged event was fired by the two-way binding code instead of by the user?
I also tried to use the DataChanged event on the XmlDataProvider but this event only appears to fire when initially loading the data.
I suppose that I could trap the key presses in the TextBox directly but it seems messy.
[Edit] It appears that PreviewTextInput seems to work for this purpose...Is there a better way?
PreviewTextInput did the trick.

Categories