1

I have a winforms application and what i want to achieve is making the Userinterface auto updates it self once the objects it is binded to is changed.

This is the what i have tried but unfortunately the text box text isn't changed automatically!

Employee employee = new Employee();

public Form1()
{
    InitializeComponent();
    textBox1.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
    textBox1.DataBindings.Add("Text", employee, "Name");
}

class Employee
{
    public string Name { get; set; }
    public int Age { get; set; }
}

private void button1_Click(object sender, EventArgs e)
{
    employee.Name = Guid.NewGuid().ToString();
}
Roman Ratskey
  • 5,101
  • 8
  • 44
  • 67
  • It seems you're looking for a MVVM framework for WinForms: http://stackoverflow.com/questions/595469/ui-design-pattern-for-windows-forms-like-mvvm-for-wpf – noseratio Feb 13 '14 at 04:34
  • 1
    Sounds like you aren't deep enough into working with WinForm that it would make a difference for you to switch to WPF. Seriously. WinForm is a seriously flawed tech. – Aron Feb 13 '14 at 06:26

1 Answers1

0

After setting the employee name in button1_Click, you could update textBox1' Text property by calling the ReadValue method to enforce textBox1 re-read the binding value.

textBox1.DataBindings[0].ReadValue();

Or, You could warp the depending employee instance into a BindingSource, and reset BindingSource in button1_Click.

Employee employee = new Employee();
BindingSource source;

public Form1()
{
    InitializeComponent();

    source = new BindingSource();
    source.DataSource = employee;

    textBox1.DataBindings.Add("Text", source, "Name");
}

private void button1_Click(object sender, EventArgs e)
{
    employee.Name = Guid.NewGuid().ToString();
    source.ResetCurrentItem();
}
ssett
  • 432
  • 3
  • 6