473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

data binding a simple value

Hi to all,

I have an object that has a property numberOfFilesDeleted which is an
integer. I want to bind a label to it so that when the value changes, the
label gets updated on the screen.

I have used the following code to bind the text property of the label to the
object property.

private void WinForm_Load(object sender, System.EventArgs e)
{
label1.DataBindings.Add(new Binding("Text", directorySweepObj,
"numberOfFilesDeleted"));
}
When I do the following :
private void button1_Click(object sender, System.EventArgs e)
{
directorySweepObj.numberOfFilesDeleted =
directorySweepObj.numberOfFilesDeleted + 1
}

I would expect the label to be updated, but it is not.

What am I doing wrong?

Sham.
Jan 31 '06 #1
3 1679
Your object needs to tell the UI that it has changed; this can be done in
two ways:

1.1 style: create a public event on your class called {propertyname}Changed,
and call this event when your values change

2 style: implement the INotifyPropertyChanged interface, and call this event
(specifying the name of the property) when the values change

Following is an example of the latter:

public class MyClass : INotifyPropertyChanged {
private int someField;
public int SomeProperty {
get {
return someField;
}
set {
if (someField != value) {
someField = value;
OnPropertyChanged("SomeProperty"); // oh for missing the
nameof(SomeProperty) {or nameof(method)?} function...
}
}
}
protected void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
Jan 31 '06 #2
Hi Marc,

Thanks for the reply.

I just started looking at C# last month in my spare time. So it looks like I
completely misunderstood databinding. I thought that was the whole point of
it (i.e. some sort of subject-observer pattern), where by specifying the
binding would handle the updating, registering of observer, notification
etc.

The code I have seen in the past with databinding was always with dataSets
and not with object properties.

So, I still need the data binding code -> label1.DataBindings.Add(new
Binding("Text", directorySweepObj,
"numberOfFilesDeleted"));

and I then introduce the notification code in my object? Is that correct?

Thanks once again.
Sham.

"Marc Gravell" <mg******@rm.com> wrote in message
news:uf**************@TK2MSFTNGP09.phx.gbl...
Your object needs to tell the UI that it has changed; this can be done in
two ways:

1.1 style: create a public event on your class called
{propertyname}Changed, and call this event when your values change

2 style: implement the INotifyPropertyChanged interface, and call this
event (specifying the name of the property) when the values change

Following is an example of the latter:

public class MyClass : INotifyPropertyChanged {
private int someField;
public int SomeProperty {
get {
return someField;
}
set {
if (someField != value) {
someField = value;
OnPropertyChanged("SomeProperty"); // oh for missing
the nameof(SomeProperty) {or nameof(method)?} function...
}
}
}
protected void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}

Jan 31 '06 #3
Correct;

IIRC DataSets already include this functionality, which is why you haven't
seen it so far. If you think about it. it makes sense: you don't want the UI
constantly probing your objects to see which properties have changed.

For ref, if you bing to a BindingSource instead (which sits between the UI
and the objects) you can call ResetBindings() on the source to force a
re-read from the object (e.g. when you think it has changed), but IMO it is
better to use notification - this avoids a lot of problems.

Marc
Jan 31 '06 #4

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

Similar topics

0
by: Ann Morris | last post by:
INTRODUCTION One of the most powerful aspects of .NET and Windows Forms is data binding. Data binding is the process of associating user interface (UI) elements with a data source to generate a...
6
by: Andy Barber | last post by:
Hi, I'm trying to write an app that reads data from a table into a string variable for later use in my program. Below is a snippet of the code I'm using, which compiles ok, but at runtime I get...
4
by: Alan Silver | last post by:
Hello, I'm trying to use an ArrayList to do data binding, but am getting an error I don't understand. I posted this in another thread, but that was all confused with various other problems,...
2
by: Tubs | last post by:
I am trying to use data binding to avoid having to write event handlers everywhere for my data transfer from controls to data source and all is working well except, i have a condition i would like...
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...
0
by: Larry Serflaten | last post by:
I am not sure how many are aware of this sort of data binding, but as it is new to many (classic) VB developers I thought I would post this once just to let people know of its availablility. ...
2
by: Matthias | last post by:
Hi Team this may be a newbie question. I have searched the discussions before posting: I'd like to re-use a form instance to edit record details. My data binding (a DataTable bound to...
0
by: mjsterz | last post by:
I've been working with VB .NET for less than a year and this is the first time I've posted on one of these groups, so let me apologize beforehand if I'm being unclear, not posting my issue...
14
by: Rolf Welskes | last post by:
Hello, I have an ObjectDataSource which has as business-object a simple array of strings. No problem. I have an own (custom) control to which I give the DataSourceId and in the custom-control...
9
by: Anil Gupte | last post by:
After reading a tutorial and fiddling, I finally got this to work. I can now put two tables created with a DataTable class into a DataRelation. Phew! And it works! Dim tblSliceInfo As New...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
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...

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.