Binding to data from controls

  • Thread starter Thread starter Michael Gorbach
  • Start date Start date
M

Michael Gorbach

Iv got a settings object (class) for my program that is beginning to
get pretty complicated and large. Im begining to get tired of
maintaining a user interface with functions that put and read fields
and propoerties of this object to/from textboxes. Can I use some kind
of data binding to either the textboxes or a grid to have them pull
data from my SimulationSettings object's properties without me having
to copy and read it textbox by textbox?
 
Michael,

Yes, you can. You can bind to a property by adding a binding to the
textbox and then specifying the object to bind to, the property, and the
field on the textbox to bind to, like so:

// Bind the "Text" property to the "Setting" property on the class passed
in.
textBox.DataBindings.Add(
"Text", // The property on the textbox to bind to.
config, // The object with the property to bind to.
"Setting"); // The property on the config object to supply the value.

Now, when you change the value on the textbox, it will update the
instance of the object with this value. If you want the databinding to go
the other way (to have the textbox update when the data changes on the
object), you need to create an event of type EventHandler with the name
<Property>Changed (in this case SettingChanged). You would fire this event
after you changed the value in your property, and the binding infrastructure
will pick up on it and change the textbox appropriately.

Hope this helps.
 
One more related issue ... I have a configuraiton object that holds an
array of particle objects. how can i set a table that will show
properties of these particle objects?
 
Back
Top