2

I have a Windows Form with a number of Textboxes. I bind them to a DataSource as the Form Loads.

public partial class FormSettings : Form
{
    private readonly DbContext _context = new DbContext();

    public FormSettings()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        _context.OrderEntrySettings.Load();

        SettingsBindingSource.DataSource = _context.OrderEntrySettings.Local.ToBindingList();

        Binding saNbinding = new Binding("Text", SettingsBindingSource, "InvoiceNumber");

        InvoiceNumberTextBox.DataBindings.Add(saNbinding);

        Binding pthNbinding = new Binding("Text", SettingsBindingSource, "SalesOrderExportPath");

        PathTextBox.DataBindings.Add(pthNbinding);

    <snip>
    …   
    </snip>
}

One of the Textboxes is bound to a Directory Path (string) Property. If I type in a new directory in the Path Textbox and hit my save button, The path saves just fine.

But, If I change the Text Property on the Textbox to the SelectedPath from a FolderBrowserDialog dialog and then hit save, the Textbox Text shows the directory I selected but the Entity is not modified and nothing gets saved back to the database.

As a workaround, I set both the Path Textbox Text and set the Property from the context to the SelectedPath.

Why does a Bound Textbox not modify its associated property if the Text value is set programmatically?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Randy
  • 1,137
  • 16
  • 49
  • where you do the binding of the working path. do the same thing when you change the path by utilizing the same binding functionality / call you need to null out the initial binding then rebind then call PathTextBox.DataBindings.Add method – MethodMan Oct 29 '15 at 18:38

2 Answers2

8

Here is the reason. Binding class has a property called DataSourceUpdateMode with 3 options - Never, OnPropertyChanged and OnValidation, the last being a default. When you set the Text property programatically, there is no validating/validated events because they fire only when the control is on focus and has been edited, so the binding does not update the data source.

That being said, here are the options you have:

(A) Do not set the control property programatically, set the data source property instead.

(B) Change the binding DataSourceUpdateMode to OnPropertyChanged. This however will cause the data source property to be updated on every char that the user is typing.

(C) Use the following snippet:

yourTextBox.Text = ...;
yourTextBox.DataBindings["Text"].WriteValue();
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • Thanks @IvanStoev, your solution also worked, this answer feels better to me than changing the focus. – Randy Oct 29 '15 at 19:27
3

If memory serves, data binding in WinForms (and possibly WebForms) uses the changes in control focus to detect when the data in the field has changed.

When you are modifying the values of the controls in the code-behind, you generally aren't changing the control focus. Consequently, there isn't anything that pokes the data-binding engine in the ribs and clues it in to the fact that it needs to re-evaluate the data.

Mike Hofer
  • 16,477
  • 11
  • 74
  • 110
  • Thanks @MikeHofer, your answer fixed my problem. I needed to set the focus to my textbox before changing the text. – Randy Oct 29 '15 at 19:23