473,472 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Binding control property to class property?

Can anybody help me with this problem, please:
I've got a class

public MyClass
{
private string m_TextValue = "";

public void SetValues()
{
m_TextValue = "Test";
}

public string TextValue
{
get { return m_TextValue; }
set { m_TextValue = value; }
}
}

and in a form I instatiate my class

private MyClass MyClassInstance = new MyClass();

I bind the TextValue property of MyClassInstance to a Textbox' Text field in
the Form_Load event:

textBox1.DataBindings.Add("Text", MyClassInstance, "TextValue");

(I can also do this using the DataBindings field in the Property editor, but
that doesn't change anything)

I would expect textbox1 to show "" now (which it does).

Then I call:
MyClassInstance.SetValues();

Then I would expect textBox1 to show "Test" (which it DOESN'T!)

But if I enter text into textBox1, it is correctly transferred to the
MyClassInstance.TextValue property, so I know the databinding works. What I
don't understand is why the SetValues method changes doesn't reflect in the
bound control's property.

What am I missing?

TIA,
Johnny Jörgensen
May 2 '07 #1
5 2263
How would the UI know about the change? You need to implement change
notification. There are 2 *primary* ways of doing this (and various
others);

a: (property specific notification)
add a notification event as follows:
public event EventHandler TextValueChanged;
change your setter to:
set {
if(value == m_TextValue) return; // do nothing if not changed
m_TextValue = value;
EventHandler handler = TextValueChanged;
if(handler!=null) handler(this, EventArgs.Empty); // notify callers
(if any)
}

[although the norm is to split out the bottom 2 into an On... method]

b: (shared notification)
implement the INotifyPropertyChanged, and simlar to above but instead
of EventArgs.Empty you'd create a new
PropertyChangedEventArgs("TextValue")

Marc
May 2 '07 #2
Note... if you use the first approach and you have more than a couple
of events you may wish to look into EventHandlerList; this reduces the
footprint as the class only reserves space for one reference field per
instance and one static reference key per event, not one per event per
instance

(i.e. when nothing is listening your placeholders consume "instances +
events" space instead of "instances * events" space).

Marc
May 2 '07 #3
Thanks Marc

I tried using approach b because I have a lot of properties.

I changed:
public class MyClass
to
public class MyClass : INotifyPropertyChanged

and added:

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}

Then I changed the Set's to:

public string TextValue
{
get { return m_TextValue; }
set
{
if (value == m_TextValue) { return; }
m_TextValue = value;
NotifyPropertyChanged("TextValue");
}
}

But still it doesn't work. What am I missing this time?

TIA,
Johnny J.



"Marc Gravell" <ma**********@gmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
How would the UI know about the change? You need to implement change
notification. There are 2 *primary* ways of doing this (and various
others);

a: (property specific notification)
add a notification event as follows:
public event EventHandler TextValueChanged;
change your setter to:
set {
if(value == m_TextValue) return; // do nothing if not changed
m_TextValue = value;
EventHandler handler = TextValueChanged;
if(handler!=null) handler(this, EventArgs.Empty); // notify callers (if
any)
}

[although the norm is to split out the bottom 2 into an On... method]

b: (shared notification)
implement the INotifyPropertyChanged, and simlar to above but instead of
EventArgs.Empty you'd create a new PropertyChangedEventArgs("TextValue")

Marc

May 2 '07 #4
A very simple oversight; SetValues should read:
TextValue = "Test"
- otherwise none of your fancy new code will ever get called. I have
tested this (below) and it works fine. As a general rule, try to only
talk to fields from property getters/setters (and constructors if
marked readonly) - that way you can't miss this type of thing. Don't
worry about performance: if the getter etc is simple then it will get
inlined by the JIT anyway.

static void Main() {
MyClass c = new MyClass();
using (Form f = new Form())
using (Button b = new Button()) {
f.Controls.Add(b);
f.DataBindings.Add("Text", c, "TextValue");
b.Click += delegate {
c.SetValues();
};
f.ShowDialog();
}
}

Marc

May 2 '07 #5
Thanks Marc - I finally got it to work. I'm so happy! ;-)
May 3 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Marina | last post by:
Here is the problem. If 2 different properties on the same (or different) control are bound to the same data column, changing the Text property and calling EndCurrentEdit discards the new value. ...
1
by: Antero | last post by:
I'm desperate! :-/. I'm desperately trying to bind to objects together to share same data and to display it in a windows forms. I'm trying to bind an custom object to custom control property. I...
2
by: Gastin | last post by:
I am consuming a web serivce from Amazon.Com. I have the following class which was autogenerated by VS.NET when I created a Web Reference to...
6
by: Scott Mueller | last post by:
I have a question about simple binding: (please forgive any syntax errors... I am at home, where I do not have Vb.Net installed, and I rely far too much on Vb's intellisense!) If I have two...
11
by: Rourke Eleven | last post by:
I have looked and searched. What good is the databind property on Radiobuttons? How does one go about actually using it? What is a good resource on this? I understand that I can easily get/set...
19
by: Simon Verona | last post by:
I'm not sure if I'm going down the correct route... I have a class which exposes a number of properties of an object (in this case the object represents a customer). Can I then use this...
1
by: A. Spiehler | last post by:
I'm trying to fill a listBox control with string members from an array of objects. I think using data binding is supposed to be the easiest way to do this. I've never used data binding before and...
6
by: Mikus Sleiners | last post by:
Is there any way to enable exception throws in VS 2005, that occur during binding operations? I am upset that i can't see exceptions that are thrown during binding operations. It's very hard to...
3
by: Simon Tamman | last post by:
I've come across an interesting bug. I have workarounds but i'd like to know the root of the problem. I've stripped it down into a short file and hope someone might have an idea about what's going...
9
by: =?Utf-8?B?VGVycnk=?= | last post by:
Think it is great the way that you can set up a datsource, value member, and display member for a combobox, and map a 'code' to a 'description' and back again by binding the combobox to a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.