Why does the following code not work (displays the correct values in
the second textbox). if i chane the OnFocusChange method to the
following it will work:
private void OnFocusChange(object sender,
System.ComponentModel.CancelEventArgs e)
{
Control control = (Control)
sender; control.BindingContext[test].EndCurrentEdit();
results.Text = test.BindingTestString;
}
but not with:
private void OnFocusChange(object sender,
System.ComponentModel.CancelEventArgs e)
{
Control control = (Control) sender;
control.BindingContext[test, "BindingTestString"].EndCurrentEdit();
results.Text = test.BindingTestString;
}
or the below code.
Thanks
Wesley
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace BindingTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
BindingTest test = new BindingTest();
private System.Windows.Forms.TextBox binder;
private System.Windows.Forms.TextBox results;
private System.Windows.Forms.Button done;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
Binding binding = new Binding("Text", test, "BindingTestString");
binder.DataBindings.Add(binding);
}
/// <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
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.binder = new System.Windows.Forms.TextBox();
this.results = new System.Windows.Forms.TextBox();
this.done = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// binder
//
this.binder.Location = new System.Drawing.Point(24, 16);
this.binder.Name = "binder";
this.binder.Size = new System.Drawing.Size(352, 20);
this.binder.TabIndex = 0;
this.binder.Text = "";
this.binder.Validating += new
System.ComponentModel.CancelEventHandler(this.OnFo cusChange);
//
// results
//
this.results.Location = new System.Drawing.Point(24, 48);
this.results.Name = "results";
this.results.Size = new System.Drawing.Size(352, 20);
this.results.TabIndex = 1;
this.results.Text = "";
//
// done
//
this.done.Location = new System.Drawing.Point(304, 80);
this.done.Name = "done";
this.done.TabIndex = 2;
this.done.Text = "DONE";
this.done.Click += new System.EventHandler(this.done_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(400, 116);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.done,
this.results,
this.binder});
this.Name = "Form1";
this.Text = "Binding Test";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void OnFocusChange(object sender,
System.ComponentModel.CancelEventArgs e)
{
Control control = (Control) sender;
control.BindingContext[control.DataBindings].EndCurrentEdit();
results.Text = test.BindingTestString;
}
private void done_Click(object sender, System.EventArgs e)
{
this.Dispose(true);
}
}
class BindingTest
{
string internalString = "default";
public BindingTest(){}
public string BindingTestString
{
get{return internalString;}
set{internalString = value;}
}
}
}