471,326 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 software developers and data experts.

Binding data to form controls

Hi,

I am trying to bind an object to form controls. I've got an object
MyObject which has some properties e.g. ID.

I've got a form, Form1, that contains a textbox ,TextBox1, now I want to
bind the value of the ID-property of a MyObject-object to the TextBox1.

I've tried doing this:

- Form1 has a private field 'private MyObject myObject;'
- Form1 has a public property 'public MyObject MyObject' to get and set
the value of 'myObject'.
- in the Form1 constructor I placed:
this.TextBox1.DataBindings.Add("Text",this.myObjec t,"ID");

I expected this to work, but when I change the value of the MyObject
property of Form1, de value of TextBox1 is not updated to the value of
the new MyObject.ID property.

What is the best way to bind the values of an object's properties to
form controls?

thanks in advance,
Dries
Dec 8 '05 #1
4 2227
See my post titled "Re: Databinding to business object" on the 1st Dec;
basically:

* To get the UI to update based on values, you need to correctly implement
the INotifyPropertyChanged interface
* You may (not 100% sure) get better results binding to a BindingSource
rather than the object directly, and then adding your object to the
BindingSource via a List<T> of your object(s)

Marc

"Dries De Rudder" <dr************@student.kuleuven.be> wrote in message
news:11***************@seven.kulnet.kuleuven.ac.be ...
Hi,

I am trying to bind an object to form controls. I've got an object
MyObject which has some properties e.g. ID.

I've got a form, Form1, that contains a textbox ,TextBox1, now I want to
bind the value of the ID-property of a MyObject-object to the TextBox1.

I've tried doing this:

- Form1 has a private field 'private MyObject myObject;'
- Form1 has a public property 'public MyObject MyObject' to get and set
the value of 'myObject'.
- in the Form1 constructor I placed:
this.TextBox1.DataBindings.Add("Text",this.myObjec t,"ID");

I expected this to work, but when I change the value of the MyObject
property of Form1, de value of TextBox1 is not updated to the value of the
new MyObject.ID property.

What is the best way to bind the values of an object's properties to form
controls?

thanks in advance,
Dries

Dec 8 '05 #2
Re-reading your post, and seeing that it is the *instance* that is changing,
and not properties, then definitely the BindingSource option will help; full
example (which works with both instance and property changes) follows - just
put in a Main method;

The "Reset" button changes the instance; the "Append" button changes a value
on the current instance. Note that this would also work (without changes)
with a form that supports navigation of multiple items - just instead of
completely wiping _source, just .Add() a new member.

Hope this helps,

Marc

public class MyClass : INotifyPropertyChanged {
private string _myField = string.Empty;
public string MyProperty {
get { return _myField; }
set {
if (_myField != value) {
_myField = value;
OnPropertyChanged("MyProperty");
}
}
}
protected void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Form1 : Form {
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button reset, append;
private BindingSource _source;
public Form1() {
// designer init: put a textbox and 2 buttons on the form
InitializeComponent();
// initialise the data-bindings
_source = new BindingSource();
_source.DataSource = typeof(MyClass);
_source.CurrentChanged += new
EventHandler(_source_CurrentChanged);
textBox1.DataBindings.Add("Text", _source, "MyProperty");
// setup events on 2 buttons
reset.Click += delegate { Instance = new MyClass(); };
append.Click+=delegate {Instance.MyProperty+="a";};
// setup an initial object
Instance = new MyClass();
}

void _source_CurrentChanged(object sender, EventArgs e) {
_instance = (MyClass)_source.Current;
}
private MyClass _instance;
public MyClass Instance {
get { return _instance; }
set {
if (!ReferenceEquals(value, _instance)) {
_source.DataSource = new List<MyClass>(new MyClass[] {
value });
}
}
}

private void InitializeComponent() {
textBox1 = new System.Windows.Forms.TextBox();
reset = new System.Windows.Forms.Button();
append = new System.Windows.Forms.Button();
textBox1.Location = new System.Drawing.Point(20, 20);
reset.Location = new System.Drawing.Point(20, 50);
reset.Text = "Reset";
append.Location = new System.Drawing.Point(20, 80);
append.Text = "Append";
Controls.AddRange(new Control[] {textBox1,reset,append});
}
}

"Marc Gravell" <mg******@rm.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
See my post titled "Re: Databinding to business object" on the 1st Dec;
basically:

* To get the UI to update based on values, you need to correctly implement
the INotifyPropertyChanged interface
* You may (not 100% sure) get better results binding to a BindingSource
rather than the object directly, and then adding your object to the
BindingSource via a List<T> of your object(s)

Marc

"Dries De Rudder" <dr************@student.kuleuven.be> wrote in message
news:11***************@seven.kulnet.kuleuven.ac.be ...
Hi,

I am trying to bind an object to form controls. I've got an object
MyObject which has some properties e.g. ID.

I've got a form, Form1, that contains a textbox ,TextBox1, now I want to
bind the value of the ID-property of a MyObject-object to the TextBox1.

I've tried doing this:

- Form1 has a private field 'private MyObject myObject;'
- Form1 has a public property 'public MyObject MyObject' to get and set
the value of 'myObject'.
- in the Form1 constructor I placed:
this.TextBox1.DataBindings.Add("Text",this.myObjec t,"ID");

I expected this to work, but when I change the value of the MyObject
property of Form1, de value of TextBox1 is not updated to the value of
the new MyObject.ID property.

What is the best way to bind the values of an object's properties to form
controls?

thanks in advance,
Dries


Dec 8 '05 #3
The BindingSource works perfect!!

thanks a lot

Marc Gravell wrote:
Re-reading your post, and seeing that it is the *instance* that is changing,
and not properties, then definitely the BindingSource option will help; full
example (which works with both instance and property changes) follows - just
put in a Main method;

The "Reset" button changes the instance; the "Append" button changes a value
on the current instance. Note that this would also work (without changes)
with a form that supports navigation of multiple items - just instead of
completely wiping _source, just .Add() a new member.

Hope this helps,

Marc

public class MyClass : INotifyPropertyChanged {
private string _myField = string.Empty;
public string MyProperty {
get { return _myField; }
set {
if (_myField != value) {
_myField = value;
OnPropertyChanged("MyProperty");
}
}
}
protected void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Form1 : Form {
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button reset, append;
private BindingSource _source;
public Form1() {
// designer init: put a textbox and 2 buttons on the form
InitializeComponent();
// initialise the data-bindings
_source = new BindingSource();
_source.DataSource = typeof(MyClass);
_source.CurrentChanged += new
EventHandler(_source_CurrentChanged);
textBox1.DataBindings.Add("Text", _source, "MyProperty");
// setup events on 2 buttons
reset.Click += delegate { Instance = new MyClass(); };
append.Click+=delegate {Instance.MyProperty+="a";};
// setup an initial object
Instance = new MyClass();
}

void _source_CurrentChanged(object sender, EventArgs e) {
_instance = (MyClass)_source.Current;
}
private MyClass _instance;
public MyClass Instance {
get { return _instance; }
set {
if (!ReferenceEquals(value, _instance)) {
_source.DataSource = new List<MyClass>(new MyClass[] {
value });
}
}
}

private void InitializeComponent() {
textBox1 = new System.Windows.Forms.TextBox();
reset = new System.Windows.Forms.Button();
append = new System.Windows.Forms.Button();
textBox1.Location = new System.Drawing.Point(20, 20);
reset.Location = new System.Drawing.Point(20, 50);
reset.Text = "Reset";
append.Location = new System.Drawing.Point(20, 80);
append.Text = "Append";
Controls.AddRange(new Control[] {textBox1,reset,append});
}
}

"Marc Gravell" <mg******@rm.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
See my post titled "Re: Databinding to business object" on the 1st Dec;
basically:

* To get the UI to update based on values, you need to correctly implement
the INotifyPropertyChanged interface
* You may (not 100% sure) get better results binding to a BindingSource
rather than the object directly, and then adding your object to the
BindingSource via a List<T> of your object(s)

Marc

"Dries De Rudder" <dr************@student.kuleuven.be> wrote in message
news:11***************@seven.kulnet.kuleuven.ac. be...
Hi,

I am trying to bind an object to form controls. I've got an object
MyObject which has some properties e.g. ID.

I've got a form, Form1, that contains a textbox ,TextBox1, now I want to
bind the value of the ID-property of a MyObject-object to the TextBox1.

I've tried doing this:

- Form1 has a private field 'private MyObject myObject;'
- Form1 has a public property 'public MyObject MyObject' to get and set
the value of 'myObject'.
- in the Form1 constructor I placed:
this.TextBox1.DataBindings.Add("Text",this.myOb ject,"ID");

I expected this to work, but when I change the value of the MyObject
property of Form1, de value of TextBox1 is not updated to the value of
the new MyObject.ID property.

What is the best way to bind the values of an object's properties to form
controls?

thanks in advance,
Dries



Dec 8 '05 #4
you're welcome

"Dries De Rudder" <dr************@student.kuleuven.be> wrote in message
news:11***************@seven.kulnet.kuleuven.ac.be ...
The BindingSource works perfect!!

thanks a lot

Marc Gravell wrote:
Re-reading your post, and seeing that it is the *instance* that is
changing, and not properties, then definitely the BindingSource option
will help; full example (which works with both instance and property
changes) follows - just put in a Main method;

The "Reset" button changes the instance; the "Append" button changes a
value on the current instance. Note that this would also work (without
changes) with a form that supports navigation of multiple items - just
instead of completely wiping _source, just .Add() a new member.

Hope this helps,

Marc

public class MyClass : INotifyPropertyChanged {
private string _myField = string.Empty;
public string MyProperty {
get { return _myField; }
set {
if (_myField != value) {
_myField = value;
OnPropertyChanged("MyProperty");
}
}
}
protected void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Form1 : Form {
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button reset, append;
private BindingSource _source;
public Form1() {
// designer init: put a textbox and 2 buttons on the form
InitializeComponent();
// initialise the data-bindings
_source = new BindingSource();
_source.DataSource = typeof(MyClass);
_source.CurrentChanged += new
EventHandler(_source_CurrentChanged);
textBox1.DataBindings.Add("Text", _source, "MyProperty");
// setup events on 2 buttons
reset.Click += delegate { Instance = new MyClass(); };
append.Click+=delegate {Instance.MyProperty+="a";};
// setup an initial object
Instance = new MyClass();
}

void _source_CurrentChanged(object sender, EventArgs e) {
_instance = (MyClass)_source.Current;
}
private MyClass _instance;
public MyClass Instance {
get { return _instance; }
set {
if (!ReferenceEquals(value, _instance)) {
_source.DataSource = new List<MyClass>(new MyClass[]
{ value });
}
}
}

private void InitializeComponent() {
textBox1 = new System.Windows.Forms.TextBox();
reset = new System.Windows.Forms.Button();
append = new System.Windows.Forms.Button();
textBox1.Location = new System.Drawing.Point(20, 20);
reset.Location = new System.Drawing.Point(20, 50);
reset.Text = "Reset";
append.Location = new System.Drawing.Point(20, 80);
append.Text = "Append";
Controls.AddRange(new Control[] {textBox1,reset,append});
}
}

"Marc Gravell" <mg******@rm.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
See my post titled "Re: Databinding to business object" on the 1st Dec;
basically:

* To get the UI to update based on values, you need to correctly
implement the INotifyPropertyChanged interface
* You may (not 100% sure) get better results binding to a BindingSource
rather than the object directly, and then adding your object to the
BindingSource via a List<T> of your object(s)

Marc

"Dries De Rudder" <dr************@student.kuleuven.be> wrote in message
news:11***************@seven.kulnet.kuleuven.ac .be...

Hi,

I am trying to bind an object to form controls. I've got an object
MyObject which has some properties e.g. ID.

I've got a form, Form1, that contains a textbox ,TextBox1, now I want to
bind the value of the ID-property of a MyObject-object to the TextBox1.

I've tried doing this:

- Form1 has a private field 'private MyObject myObject;'
- Form1 has a public property 'public MyObject MyObject' to get and set
the value of 'myObject'.
- in the Form1 constructor I placed:
this.TextBox1.DataBindings.Add("Text",this.myO bject,"ID");

I expected this to work, but when I change the value of the MyObject
property of Form1, de value of TextBox1 is not updated to the value of
the new MyObject.ID property.

What is the best way to bind the values of an object's properties to
form controls?

thanks in advance,
Dries


Dec 8 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Marcin Floryan | last post: by
1 post views Thread by JD Kronicz | last post: by
reply views Thread by popsovy | last post: by
5 posts views Thread by Vigneshwar Pilli via DotNetMonster.com | last post: by
11 posts views Thread by Rourke Eleven | last post: by
reply views Thread by Larry Serflaten | last post: by
2 posts views Thread by Matthias | last post: by
19 posts views Thread by Larry Lard | last post: by
3 posts views Thread by Simon Tamman | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.