473,405 Members | 2,279 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,405 software developers and data experts.

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.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace BPDescriptorTests
{
/// <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.ComponentModel.Container components = null;

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

//
// TODO: Add any constructor code after InitializeComponent call
//
this.textBox2.Text = myInt.ToString();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
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(object sender, System.EventArgs e)
{
myInt ++;
this.textBox2.Text = myInt.ToString();
}

private void BoundForm_Load(object sender, System.EventArgs e)
{
this.textBox1.DataBindings.Add("Text",myInt,null);
}
}
}
Nov 16 '05 #1
6 1567
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.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace BPDescriptorTests
{
/// <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.ComponentModel.Container components = null;
public BoundForm()
{
//
// Required for Windows Form Designer support //
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call //
this.textBox2.Text = myInt.ToString(); }

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing ) {
if( disposing )
{
if(components != null)
{
components.Dispose (); }
}
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(object sender, System.EventArgs e) {
myInt ++;
this.textBox2.Text = myInt.ToString(); }

private void BoundForm_Load(object sender, System.EventArgs e) {
this.textBox1.DataBindings.Add ("Text",myInt,null); }
}
}
.

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 misunderstanding, 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.Add
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.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace BPDescriptorTests
{
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.ComponentModel.Container components = null;

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

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

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
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(object sender, System.EventArgs e)
{
int i;
i = p.Id + 1;
p.IdStr = System.Convert.ToString(i);

this.textBox2.Text = p.IdStr;
}

private void BoundForm_Load(object sender, System.EventArgs e)
{
textBox1.DataBindings.Add("Text",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 misunderstanding, 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
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
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...
19
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...
4
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. ...
2
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...
7
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...
4
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...
0
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...
0
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. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
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.