473,565 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stumped by bound controls

BBM
Hi,

I'm having trouble getting control data binding to work. As I understand
it, the simplest form of databinding requires three things:

1) A control (say a textbox) on a form or user control,
2) A field or object.member to which you want to bind,
3) A "Binding" object that connects the variable to a property of the
control ("Text" for example).

The example that follows does these three things, but the data binding
doesn't work (at least not like it think it should). It couldn't be simpler.
I have a form with two textboxes. One is "bound" to the field myInt. The
other has it's "Text" property set to myInt.ToString( ) each time myInt
changes. myInt starts with a value of 200. There is a button on the form
that increments myInt.

Now I expect that the value displayed should stay in synch between the two
textboxes. But, although they start in synch (both showing "200"),
incrementing myInt by clicking the button changes the value in the textbox
that has "Text" explicitly changed, but my bound textbox continues to display
"200".

I'm obviously missing something very basic here.

Thanks.

BBM

using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;

namespace BPDescriptorTes ts
{
/// <summary>
/// Summary description for BoundForm.
/// </summary>
public class BoundForm : System.Windows. Forms.Form
{
private System.Windows. Forms.TextBox textBox1;
private System.Windows. Forms.Button button1;
public int myInt = 200;
private System.Windows. Forms.TextBox textBox2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;

public BoundForm()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();

//
// TODO: Add any constructor code after InitializeCompo nent call
//
this.textBox2.T ext = myInt.ToString( );
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
// This stuff looked standard so I omitted it... BBM
#endregion

static void Main()
{
Application.Run (new BoundForm());
}

private void button1_Click(o bject sender, System.EventArg s e)
{
myInt ++;
this.textBox2.T ext = myInt.ToString( );
}

private void BoundForm_Load( object sender, System.EventArg s e)
{
this.textBox1.D ataBindings.Add ("Text",myInt,n ull);
}
}
}
Nov 16 '05 #1
6 1577
I've not done a lot with ADO.Net and bound objects, but I
can tell you that in VB-6 and before you needed to have
a "data control" also. This supplied the cursor to allow
access to single records and you then bound to individula
columns in bound controls. Check the ADO models, I think
you'll find a similar requirement.
-----Original Message-----
Hi,

I'm having trouble getting control data binding to work. As I understandit, the simplest form of databinding requires three things:
1) A control (say a textbox) on a form or user control,
2) A field or object.member to which you want to bind,
3) A "Binding" object that connects the variable to a property of thecontrol ("Text" for example).

The example that follows does these three things, but the data bindingdoesn't work (at least not like it think it should). It couldn't be simpler. I have a form with two textboxes. One is "bound" to the field myInt. Theother has it's "Text" property set to myInt.ToString( ) each time myIntchanges. myInt starts with a value of 200. There is a button on the formthat increments myInt.

Now I expect that the value displayed should stay in synch between the twotextboxes. But, although they start in synch (both showing "200"),incrementing myInt by clicking the button changes the value in the textboxthat has "Text" explicitly changed, but my bound textbox continues to display"200".

I'm obviously missing something very basic here.

Thanks.

BBM

using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;

namespace BPDescriptorTes ts
{
/// <summary>
/// Summary description for BoundForm.
/// </summary>
public class BoundForm : System.Windows. Forms.Form
{
private System.Windows. Forms.TextBox textBox1; private System.Windows. Forms.Button button1; public int myInt = 200;
private System.Windows. Forms.TextBox textBox2; /// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;
public BoundForm()
{
//
// Required for Windows Form Designer support //
InitializeCompo nent();

//
// TODO: Add any constructor code after InitializeCompo nent call //
this.textBox2.T ext = myInt.ToString( ); }

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing ) {
if( disposing )
{
if(components != null)
{
components.Disp ose (); }
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code// This stuff looked standard so I omitted it... BBM
#endregion

static void Main()
{
Application.Run (new BoundForm());
}

private void button1_Click(o bject sender, System.EventArg s e) {
myInt ++;
this.textBox2.T ext = myInt.ToString( ); }

private void BoundForm_Load( object sender, System.EventArg s e) {
this.textBox1.D ataBindings.Add ("Text",myInt,n ull); }
}
}
.

Nov 16 '05 #2
Hi BBM,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when you bind a textbox to a int
variable. If there is any misunderstandin g, please feel free to let me know.

This doesn't work, because int is a value type. The int object is on the
function stack. When you add a a data binding to the control, it is
actually binding to a copy of the original int. So when your int increases,
the text of TextBox doesn't change. So my recommendation is to set
TextBox.Text property again each time your int changes. It is the simplest
way.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #3
BBM
Kevin,

Thanks for your response. I see how DataBinding.Add used a "boxed" copy of
the real int. However...

My "real" requirement is to bind textbox.Text to a data member of an object.
I'd like to bind Obj.Id, an int property of the object to the textbox "Text"
property. I've tried this a bunch of different ways, including binding to
"string" versions of integer properties (Obj.IdString for example that does
all the necessary conversions).

So I haven't been able to get it to work whether I bound to a value type or
a reference type. I've tried it using two parms of the DataBindings.Ad d
method ("Text", Obj.IdStr, null) and all three parms ("Text", Obj, "IdStr").

Attached is a slightly changed version of my original example. I have added
a very simple object, and attempt to bind the first textbox to a string
property of the object. It doesn't work.

Of course I could explicitly set the values of controls for every object
attribute but I thought that databinding was supposed to do this without
having to write all that code.

Thanks.

BBM

using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;

namespace BPDescriptorTes ts
{
public class Person
{
int id;
string name;
public int Id
{
get{ return id;}
set{id = value;}
}
public string IdStr
{
get{ return id.ToString();}
set{ id = System.Convert. ToInt32(value); }
}
public Person(int i, string n)
{
id = i;
name = n;
}

}
/// <summary>
/// Summary description for BoundForm.
/// </summary>
public class BoundForm : System.Windows. Forms.Form
{
private System.Windows. Forms.TextBox textBox1;
private System.Windows. Forms.Button button1;
public int myInt = 200;
public Person p;
private System.Windows. Forms.TextBox textBox2;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;

public BoundForm()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();

//
// TODO: Add any constructor code after InitializeCompo nent call
//
p = new Person(200, "George Bush");
this.textBox2.T ext = p.IdStr;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
// Removed this stuff... BBM
#endregion

static void Main()
{
Application.Run (new BoundForm());
}

private void button1_Click(o bject sender, System.EventArg s e)
{
int i;
i = p.Id + 1;
p.IdStr = System.Convert. ToString(i);

this.textBox2.T ext = p.IdStr;
}

private void BoundForm_Load( object sender, System.EventArg s e)
{
textBox1.DataBi ndings.Add("Tex t",p,"IdStr" );
}
}
}
"Kevin Yu [MSFT]" wrote:
Hi BBM,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when you bind a textbox to a int
variable. If there is any misunderstandin g, please feel free to let me know.

This doesn't work, because int is a value type. The int object is on the
function stack. When you add a a data binding to the control, it is
actually binding to a copy of the original int. So when your int increases,
the text of TextBox doesn't change. So my recommendation is to set
TextBox.Text property again each time your int changes. It is the simplest
way.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #4
aaa
I think you need to implement IdStrChenganged event. You can find
example (quite similar to yours actually) here:

http://www.dotnet247.com/247referenc...55/278264.aspx

Nov 16 '05 #5
aaa
aa*@bbb.hr wrote:
I think you need to implement IdStrChenganged event. You can find


IdStrChanged :)

Nov 16 '05 #6
BBM
aaa,

Thanks for your response. Yes, that's exactly what was wrong. I
implemented the
<propertyname > changed event and got it to work.

The book C# Business Objects says that what I was trying will work as long
as the value of the property is not changed in CODE. To catch this you have
to use the "changed" event.

Also thanks for the reference to the dotnet247.com discussion site I think
I'll give it a try the next time I have a problem.

Thanks again.

BBM

"aa*@bbb.hr " wrote:
I think you need to implement IdStrChenganged event. You can find
example (quite similar to yours actually) here:

http://www.dotnet247.com/247referenc...55/278264.aspx

Nov 16 '05 #7

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

Similar topics

2
2402
by: Hal Davison | last post by:
I have read that most people do not like bound controls? What is the reasoning for this point of view? -- Hal Davison Davison Consulting
4
3566
by: Aaron Ackerman | last post by:
I am using typed datasets in an N-Tier Windows app using VB.NET. I know this posting cannot be fully explained in a single post that is why I am asking for someone to point me to a real world tutorial (in vb.net) that can show me how to use a bound Datagrid to it's fullest potential. I need to utilize the bound Datagrid in two modes. One...
19
4078
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can...
4
1756
by: kaborka | last post by:
I have a WinForm with controls bound to a typed recordset that was generated by the Dataset Designer. There are several ComboBox controls with their DataSource bound to different lookup tables. Everything is working fine, except when I need to handle a null default value in the data record coming from the database. I want the combo whose...
2
2056
by: Alfredo Barrientos | last post by:
Hi, I have been reading information about asp.net webcontrols, and i have found a term "List bound controls". What are "List Bound Controls"?? Which are the differences between these controls and the others?? how many webcontrols are list bound controls?? any url in microsoft.com talking about lista bound controls??
7
4203
by: Andrew McKendrick | last post by:
Hi, I've noticed a bug in VB.NET (latest .NET Framework)... - I have a TabControl on a form with several tabs. - Each tab contains text boxes that are bound to fields in a data source (DataBindings). - When I display a record and then try to access the .Text property of one of the text boxes on any tab except the current tab, the result...
4
4605
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box bound to a table as a lookup, drawing values from another table to populate the available selections. This all worked fine in VB6. I have...
0
1450
by: HP | last post by:
Hi there I have a datalist control with some bound controls in its Item Template and a gridview bound to one of those fields (residing also in Item Template). I've found out that when I click Edit on the gridview (same happens when editing other gridview which is outside the datalist) all datalist bound fields (except the gridview)...
0
2485
by: Mike | last post by:
So here's the situation (.NET 2.0 btw): I have a form, and on this form is a textbox among many other databound controls. The textbox is bound to a field in a data table via the Text property. In this table there are multiple columns that cannot be NULL, which, are bound to other controls (but they're not really important at this time). ...
0
7666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6260
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
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 we have to send another system
1
1201
muto222
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.