cdx11356

I'm looking for the correct way for me to bind an object to a winform to display some of it's attributes. For Example a Person instance with first and last name. I've been able to do it using a BindingSource, but have run into several things that I don't like.
1. The bindingSource is working with a collection, I just want to have one "person" that I'm displaying on a winform. If I change the person, I want to just replace that instance, not add to a bindingSource and navigate. I don't want the navigating tool on the form either.
2. I want to have changes that are made to the current "person" flow up to the interface. A simple example would be a button, that changes the person's name directly. I use this as an example to demonstrate what I am looking for.

I don't want the Person object connected to a database, since my application will have multiple ways to create this and I am not concerned at this point about saving the data. This application is more geared toward showing an object while processing is being done and having that information flow up to the WinForm for the user to see progress.

Thanks.




Re: Windows Forms General Help with Winform and binding to an object

Figo Fei - MSFT

Hi,

So, "person" is sth in the DB, or just a variable

What do you mean by showing "person" on winform You mean, for example, showing the attributes of "person" on the labels of the form

What is the specific senario, can you describe it clearer Thank you






Re: Windows Forms General Help with Winform and binding to an object

cdx11356

Hi,
"person" is just a variable. It contains a firstName and lastName. I am displaying those on the winForm. I want to be able to easily replace the "person" and have the firstName/lastName update on the form (as well as have the user be able to change them from the form).

I also want to be able to change the firstName programmatically in the "person" and have that change reflected in the winForm. I do not want to do this by changing the text on the form, but by changing the firstName on the "person" directly.

I am giving you an example application in order to make this clearer. The application I'm writing will be doing some processing in the background (in this case it is the "person" who is doing the processing) and I want to have it's attributes reflected on the winForm.

Thanks for your help







Re: Windows Forms General Help with Winform and binding to an object

Peter Ritchie

Your question is not specific to the C# Language. Moved from Visual C# Language forum to the Windows Forms General forum.




Re: Windows Forms General Help with Winform and binding to an object

BonnieB

Could you post some code how you've tried this already DataBinding should work so perhaps you're missing something ....




Re: Windows Forms General Help with Winform and binding to an object

cdx11356

1. I'd like to do this without the navigator, I only want to display one "person" at a time
2. When I hit button2_Click, the ui does not change to reflect the change made to firstName


namespace ConnectDataInAnObject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

if (firstNameTextBox.Text == "Chip") {
personBindingSource.Add(Person.makeBon());
} else {
personBindingSource.Add(Person.makeChip());
}
}
private void button2_Click(object sender, EventArgs e)
{
int x = 0;
Person aPerson = (Person) personBindingSource.Current;
aPerson.firstName = "XXXX";
}
}
}
using System;
using System.Collections.Generic;
using System.Text;

namespace ConnectDataInAnObject
{
class Person
{
private String _firstName;
private String _lastName;

public Person() {
_firstName = "";
_lastName = "";
}
public String firstName
{
get { return _firstName; }
set { _firstName = value; }
}
public String lastName
{
get { return _lastName; }
set { _lastName = value; }
}
public static Person makePerson(String tFirst, String tLast)
{
Person aPerson = new Person();
aPerson.firstName = tFirst;
aPerson.lastName = tLast;
return aPerson;
}
public static Person makeChip() {
return Person.makePerson("Chip", "Dice");
}
public static Person makeBon()
{
return Person.makePerson("bonnie", "Dice");
}
}
}







Re: Windows Forms General Help with Winform and binding to an object

BonnieB

You're right ... it's not working and as far as I can see, it should. I wonder if it has anything to do with the static nature of your Person class

I spent about a half hour looking for workarounds, but didn't hit on anything.






Re: Windows Forms General Help with Winform and binding to an object

timvw

Simple databinding will lead to the effect that changes in the control influence the object...

eg: this.label1.DataBindings.Add("Text", myObject, "Label"); // label is a property on myObject...

If you want changes in the object to appear in the control you have to implement INotifyPropertyChanged (and eventually set your bindingsource to raise such events...)

And then implement your object properties as following:

class MyObject : INotifyPropertyChanged
{
private event PropertyChangedEventHandler propertyChanged;

public virtual event PropertyChangedEventHandler PropertyChanged
{
add { this.propertyChanged += value; }
remove { this.propertyChanged -= value; }
}

 protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.propertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}

 public virtual string Label
{
get { return this.label; }
set
{
if (this.label != value)
{
this.label = value;
this.OnPropertyChanged("Label");
}
}
}






Re: Windows Forms General Help with Winform and binding to an object

cdx11356

ok,
So I finally got to implementing this and it works, however how do I get the change notification if I replace the entire person I think it was held in myObject in your example. So when I replace myObject with an entirely new person.

Thanks for your help,

Chip





Re: Windows Forms General Help with Winform and binding to an object

timvw

In that case i'd recommend to use a BindingSource...

BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = myObject;

this.label1.DataBindings.Add("Text", bindingSource, "MyObjectPropertyName");

// bind other ui components to the bindingsource...


And now if you want to replace myObject with myObject2 you simply do it via the DataSource property on the Bindingsource:

BindingSource bindingSource = this.label1.DataBindings["Text"].DataSource as BindingSource;
bindingSource.DataSource = myObject2;
// done ;))





Re: Windows Forms General Help with Winform and binding to an object

cdx11356

Tim,
That seems to do the trick. Thank you very much for your help.

Chip





Re: Windows Forms General Help with Winform and binding to an object

cdx11356

ok,
seems like I keep getting deeper. The app I'm writing is going to process some data in the background. Part of what it is doing will be to update some of it's instance variables, which are bound to the UI. I wanted to have the processing done in a separate thread, so the user could see the UI update and also control it (cancel, pause, etc). However it seems like this is a problem from a threaded process. Looks like I need to use the Invoke method. Does anyone know how I do this while using the BindingSource technique shown above

Thanks