G
Guest
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);
}
}
}
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);
}
}
}