all groups > dotnet windows forms databinding > september 2007 >
You're in the

dotnet windows forms databinding

group:

Beginner


Beginner Marco Schwarz
9/14/2007 3:30:03 PM
dotnet windows forms databinding: I try to use Databinding with the BindingList. So I write a class
Person(Name, ReadOnly) and a PersonList : BindingList<Person>.

In my form I have two controls (TextBox and CheckBox) in the
MainFormLoad function I binding as follow:

void MainFormLoad(object sender, EventArgs e)
{
personList = new PersonList();
cbxPersonReadOnly.DataBindings.Add("Checked", personList, "ReadOnly");
txtPersonName.DataBindings.Add("Text", personList, "Name");
txtPersonName.DataBindings.Add("ReadOnly", personList, "ReadOnly");
}

My question:

I click on the checkbox (ReadOnly) and the TextBox don't changed. Then
click on Next or Prev and the TextBox.ReadOnly is changed.

Can someone says me, what I do wrong?

Thanks
Re: Beginner Marco Schwarz
9/14/2007 3:44:11 PM
[quoted text, click to view]
Sorry, now it works correctly. Perhaps I run a old version of the
application.

Thanks
Re: Beginner Marco Schwarz
9/14/2007 4:24:13 PM
Marco Schwarz schrieb:
[quoted text, click to view]
Only with the Event

void CbxPersonReadOnlyClick(object sender, EventArgs e)
{
txtPersonName.ReadOnly = cbxPersonReadOnly.Checked;
}

it works fine.

Have someone an idea?

Thx
Re: Beginner Morten Wennevik [C# MVP]
9/15/2007 12:00:00 AM
On Fri, 14 Sep 2007 15:30:03 +0200, Marco Schwarz <marco.schwarz@rolmail=
[quoted text, click to view]

Hi Marco,

It's a combination of things that is causing your TextBox to update late=
..

First, the default databinding behaviour is to update its underlying sou=
rce once you validate the control for instance by leaving it. Since you=
want the TextBox to gray out immediatly as you click the CheckBox you n=
eed to tell the databinding to update immediatly (see the code below on =
how to do this).

Second, since you bind two controls to the same property the second cont=
rol won't get notified, and won't get updated until you cange the dataso=
urce by selecting another item or similar. To fix this, Person needs to=
implement the INotifyPropertyChanged event, and fire an event every tim=
e this property is changed. This is also true for any property you chan=
ge programmatically (see the code below on how to implement and use INot=
ifyPropertyChanged)

public partial class Form2 : System.Windows.Forms.Form
{

public Form2()
{
InitializeComponent();
}

protected override void OnLoad(EventArgs e)
{
// You probably don't need a PersonList class
BindingList<Person> personList =3D new BindingList<Person>(=
);
personList.Add(new Person("One"));
personList.Add(new Person("Two"));
personList.Add(new Person("Three"));

// The ListBox will allow us to change persons and see the =
effect of setting ReadOnly
listBox1.DataSource =3D personList;
listBox1.DisplayMember =3D "Name";

// Note the added parameters for the Checked property
// Setting DataSourceUpdateMode.OnPropertyChanged means the=
Person will get updated immediatly.
// This is also good on TextBoxes where you want to update =
something while you write.
checkBox1.DataBindings.Add("Checked", personList, "ReadOnly=
", false, DataSourceUpdateMode.OnPropertyChanged);
textBox1.DataBindings.Add("Text", personList, "Name");
textBox1.DataBindings.Add("ReadOnly", personList, "ReadOnly=
");
}
}

public class Person : INotifyPropertyChanged
{
private bool readOnly;

public bool ReadOnly
{
get { return readOnly; }
set
{
readOnly =3D value;
// Fire a PropertyChanged event so the TextBox will get=
notified
PropertyChanged(this, new PropertyChangedEventArgs("Loc=
ked"));
}
}

private string name;

public string Name
{
get { return name; }
set { name =3D value; }
}

public Person(string name)
{
this.Name =3D name;
}

// This is the code added by SHIFT-ALT-F10 on INotifyPropertyCh=
anged
// All this interface does is declare a single event
#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

#endregion
}

-- =

Happy coding!
Re: Beginner Marco Schwarz
9/15/2007 12:00:00 AM
AddThis Social Bookmark Button