How to bind the same source object in two forms without synchroniz

G

Guest

Hi,

Context:
-----------
I have two forms and one Customer class.

Form1 is made to view information about a Customer object. Controls in Form1
are bound to an instance of a Customer class. Form1 contains a button to open
Form2 in modal mode.

Form 2 is made to edit a Customer object. Controls in Form2 are bound the
same Customer instance as the Form1.

Problem:
-----------
My problem is that when Form2 is active, typing and tabbing in the controls
of Form2 cause controls in the Form 1 to be updated too. I would like the
binding in Form2 to be independent from the binding in Form1. When the user
clicks OK in form 2, then I would "repaint" Form1 to reflect the new changes.

I need to use the same Customer instance in both forms.

Can you help?


To Test:
-----------
I have included the source code of 3 classes (below) to reproduce the
problem. To run it, create an empty Windows Application Project and add the 3
files. Start the application. Click to "open modal form" button to open the
2nd form. Move the new form so that you can see both forms. Type text in the
FirstName and tab to the LastName control. As you can see the FirstName
control in Form1 is updated. This is my problem.

Regards,

-Sylvain



***************
Customer.cs
***************

using System;

namespace BindSameObjectIn2Forms
{
public class Customer
{
private string _firstName = "";
private string _lastName = "";

public string FirstName
{
get{ return _firstName; }
set{ _firstName = value; }
}

public string LastName
{
get{ return _lastName; }
set{ _lastName = value; }
}
}
}


**************
Form1.cs
**************


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace BindSameObjectIn2Forms
{
public class Form1 : System.Windows.Forms.Form
{
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private Customer _customer;
private System.Windows.Forms.Button cmdClose;
private System.Windows.Forms.Label lblFirstName;
private System.Windows.Forms.Label lblLastName;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button cmdOpenModalForm;

public Form1()
{
InitializeComponent();
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.cmdClose = new System.Windows.Forms.Button();
this.cmdOpenModalForm = new System.Windows.Forms.Button();
this.lblFirstName = new System.Windows.Forms.Label();
this.lblLastName = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// cmdClose
//
this.cmdClose.Location = new System.Drawing.Point(316, 236);
this.cmdClose.Name = "cmdClose";
this.cmdClose.TabIndex = 5;
this.cmdClose.Text = "Close";
this.cmdClose.Click += new System.EventHandler(this.cmdClose_Click);
//
// cmdOpenModalForm
//
this.cmdOpenModalForm.Location = new System.Drawing.Point(244, 192);
this.cmdOpenModalForm.Name = "cmdOpenModalForm";
this.cmdOpenModalForm.Size = new System.Drawing.Size(148, 23);
this.cmdOpenModalForm.TabIndex = 6;
this.cmdOpenModalForm.Text = "Open modal form";
this.cmdOpenModalForm.Click += new
System.EventHandler(this.cmdOpenModalForm_Click);
//
// lblFirstName
//
this.lblFirstName.Location = new System.Drawing.Point(128, 88);
this.lblFirstName.Name = "lblFirstName";
this.lblFirstName.Size = new System.Drawing.Size(272, 20);
this.lblFirstName.TabIndex = 7;
this.lblFirstName.Text = "label1";
//
// lblLastName
//
this.lblLastName.Location = new System.Drawing.Point(128, 120);
this.lblLastName.Name = "lblLastName";
this.lblLastName.Size = new System.Drawing.Size(272, 20);
this.lblLastName.TabIndex = 8;
this.lblLastName.Text = "label2";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 88);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 20);
this.label1.TabIndex = 9;
this.label1.Text = "First Name";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 120);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 20);
this.label2.TabIndex = 10;
this.label2.Text = "Last Name";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(524, 313);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.lblLastName);
this.Controls.Add(this.lblFirstName);
this.Controls.Add(this.cmdOpenModalForm);
this.Controls.Add(this.cmdClose);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

public Customer Customer
{
get{ return _customer; }
set{ _customer = value; }
}

private void Form1_Load(object sender, System.EventArgs e)
{
Customer = new Customer();

lblFirstName.DataBindings.Add( "Text", Customer, "FirstName" );
lblLastName.DataBindings.Add( "Text", Customer, "LastName" );
}

private void cmdClose_Click(object sender, System.EventArgs e)
{
this.Close();
}

private void cmdOpenModalForm_Click(object sender, System.EventArgs e)
{
modalForm form = new modalForm();
form.Customer = Customer;
form.ShowDialog();
}
}
}


**************
modalForm.cs
**************


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace BindSameObjectIn2Forms
{

public class modalForm : System.Windows.Forms.Form
{
private Customer _customer;
private System.Windows.Forms.TextBox txtLastName;
private System.Windows.Forms.TextBox txtFirstName;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button cmdOK;

public modalForm()
{
InitializeComponent();
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.txtFirstName = new System.Windows.Forms.TextBox();
this.txtLastName = new System.Windows.Forms.TextBox();
this.cmdOK = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// txtFirstName
//
this.txtFirstName.Location = new System.Drawing.Point(140, 24);
this.txtFirstName.Name = "txtFirstName";
this.txtFirstName.Size = new System.Drawing.Size(256, 20);
this.txtFirstName.TabIndex = 0;
this.txtFirstName.Text = "";
//
// txtLastName
//
this.txtLastName.Location = new System.Drawing.Point(140, 52);
this.txtLastName.Name = "txtLastName";
this.txtLastName.Size = new System.Drawing.Size(256, 20);
this.txtLastName.TabIndex = 1;
this.txtLastName.Text = "";
//
// cmdOK
//
this.cmdOK.Location = new System.Drawing.Point(292, 92);
this.cmdOK.Name = "cmdOK";
this.cmdOK.Size = new System.Drawing.Size(108, 23);
this.cmdOK.TabIndex = 2;
this.cmdOK.Text = "OK";
this.cmdOK.Click += new System.EventHandler(this.cmdOK_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 20);
this.label2.TabIndex = 12;
this.label2.Text = "Last Name";
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 20);
this.label1.TabIndex = 11;
this.label1.Text = "First Name";
//
// modalForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(416, 129);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.cmdOK);
this.Controls.Add(this.txtLastName);
this.Controls.Add(this.txtFirstName);
this.Name = "modalForm";
this.Text = "modalForm";
this.Load += new System.EventHandler(this.modalForm_Load);
this.ResumeLayout(false);

}
#endregion

public Customer Customer
{
get{ return _customer; }
set{ _customer = value; }
}

private void modalForm_Load(object sender, System.EventArgs e)
{
txtFirstName.DataBindings.Add( "Text", Customer, "FirstName" );
txtLastName.DataBindings.Add( "Text", Customer, "LastName" );

}

private void cmdOK_Click(object sender, System.EventArgs e)
{
this.Close();
}

}
}


***********
END OF SOURCE FILES
***********
 
S

Sijin Joseph

One solution would be to use BindingManagerBase.SuspendBinding() and
BindingManagerBase.ResumeBinding() methods

So before passing Customer to Form2 you would suspend the binding in
Form1 and resume it after Form2 closes.

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
Hi,

Context:
-----------
I have two forms and one Customer class.

Form1 is made to view information about a Customer object. Controls in Form1
are bound to an instance of a Customer class. Form1 contains a button to open
Form2 in modal mode.

Form 2 is made to edit a Customer object. Controls in Form2 are bound the
same Customer instance as the Form1.

Problem:
-----------
My problem is that when Form2 is active, typing and tabbing in the controls
of Form2 cause controls in the Form 1 to be updated too. I would like the
binding in Form2 to be independent from the binding in Form1. When the user
clicks OK in form 2, then I would "repaint" Form1 to reflect the new changes.

I need to use the same Customer instance in both forms.

Can you help?


To Test:
-----------
I have included the source code of 3 classes (below) to reproduce the
problem. To run it, create an empty Windows Application Project and add the 3
files. Start the application. Click to "open modal form" button to open the
2nd form. Move the new form so that you can see both forms. Type text in the
FirstName and tab to the LastName control. As you can see the FirstName
control in Form1 is updated. This is my problem.

Regards,

-Sylvain



***************
Customer.cs
***************

using System;

namespace BindSameObjectIn2Forms
{
public class Customer
{
private string _firstName = "";
private string _lastName = "";

public string FirstName
{
get{ return _firstName; }
set{ _firstName = value; }
}

public string LastName
{
get{ return _lastName; }
set{ _lastName = value; }
}
}
}


**************
Form1.cs
**************


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace BindSameObjectIn2Forms
{
public class Form1 : System.Windows.Forms.Form
{
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private Customer _customer;
private System.Windows.Forms.Button cmdClose;
private System.Windows.Forms.Label lblFirstName;
private System.Windows.Forms.Label lblLastName;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button cmdOpenModalForm;

public Form1()
{
InitializeComponent();
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.cmdClose = new System.Windows.Forms.Button();
this.cmdOpenModalForm = new System.Windows.Forms.Button();
this.lblFirstName = new System.Windows.Forms.Label();
this.lblLastName = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// cmdClose
//
this.cmdClose.Location = new System.Drawing.Point(316, 236);
this.cmdClose.Name = "cmdClose";
this.cmdClose.TabIndex = 5;
this.cmdClose.Text = "Close";
this.cmdClose.Click += new System.EventHandler(this.cmdClose_Click);
//
// cmdOpenModalForm
//
this.cmdOpenModalForm.Location = new System.Drawing.Point(244, 192);
this.cmdOpenModalForm.Name = "cmdOpenModalForm";
this.cmdOpenModalForm.Size = new System.Drawing.Size(148, 23);
this.cmdOpenModalForm.TabIndex = 6;
this.cmdOpenModalForm.Text = "Open modal form";
this.cmdOpenModalForm.Click += new
System.EventHandler(this.cmdOpenModalForm_Click);
//
// lblFirstName
//
this.lblFirstName.Location = new System.Drawing.Point(128, 88);
this.lblFirstName.Name = "lblFirstName";
this.lblFirstName.Size = new System.Drawing.Size(272, 20);
this.lblFirstName.TabIndex = 7;
this.lblFirstName.Text = "label1";
//
// lblLastName
//
this.lblLastName.Location = new System.Drawing.Point(128, 120);
this.lblLastName.Name = "lblLastName";
this.lblLastName.Size = new System.Drawing.Size(272, 20);
this.lblLastName.TabIndex = 8;
this.lblLastName.Text = "label2";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 88);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 20);
this.label1.TabIndex = 9;
this.label1.Text = "First Name";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 120);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 20);
this.label2.TabIndex = 10;
this.label2.Text = "Last Name";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(524, 313);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.lblLastName);
this.Controls.Add(this.lblFirstName);
this.Controls.Add(this.cmdOpenModalForm);
this.Controls.Add(this.cmdClose);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

public Customer Customer
{
get{ return _customer; }
set{ _customer = value; }
}

private void Form1_Load(object sender, System.EventArgs e)
{
Customer = new Customer();

lblFirstName.DataBindings.Add( "Text", Customer, "FirstName" );
lblLastName.DataBindings.Add( "Text", Customer, "LastName" );
}

private void cmdClose_Click(object sender, System.EventArgs e)
{
this.Close();
}

private void cmdOpenModalForm_Click(object sender, System.EventArgs e)
{
modalForm form = new modalForm();
form.Customer = Customer;
form.ShowDialog();
}
}
}


**************
modalForm.cs
**************


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace BindSameObjectIn2Forms
{

public class modalForm : System.Windows.Forms.Form
{
private Customer _customer;
private System.Windows.Forms.TextBox txtLastName;
private System.Windows.Forms.TextBox txtFirstName;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button cmdOK;

public modalForm()
{
InitializeComponent();
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.txtFirstName = new System.Windows.Forms.TextBox();
this.txtLastName = new System.Windows.Forms.TextBox();
this.cmdOK = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// txtFirstName
//
this.txtFirstName.Location = new System.Drawing.Point(140, 24);
this.txtFirstName.Name = "txtFirstName";
this.txtFirstName.Size = new System.Drawing.Size(256, 20);
this.txtFirstName.TabIndex = 0;
this.txtFirstName.Text = "";
//
// txtLastName
//
this.txtLastName.Location = new System.Drawing.Point(140, 52);
this.txtLastName.Name = "txtLastName";
this.txtLastName.Size = new System.Drawing.Size(256, 20);
this.txtLastName.TabIndex = 1;
this.txtLastName.Text = "";
//
// cmdOK
//
this.cmdOK.Location = new System.Drawing.Point(292, 92);
this.cmdOK.Name = "cmdOK";
this.cmdOK.Size = new System.Drawing.Size(108, 23);
this.cmdOK.TabIndex = 2;
this.cmdOK.Text = "OK";
this.cmdOK.Click += new System.EventHandler(this.cmdOK_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 20);
this.label2.TabIndex = 12;
this.label2.Text = "Last Name";
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 20);
this.label1.TabIndex = 11;
this.label1.Text = "First Name";
//
// modalForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(416, 129);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.cmdOK);
this.Controls.Add(this.txtLastName);
this.Controls.Add(this.txtFirstName);
this.Name = "modalForm";
this.Text = "modalForm";
this.Load += new System.EventHandler(this.modalForm_Load);
this.ResumeLayout(false);

}
#endregion

public Customer Customer
{
get{ return _customer; }
set{ _customer = value; }
}

private void modalForm_Load(object sender, System.EventArgs e)
{
txtFirstName.DataBindings.Add( "Text", Customer, "FirstName" );
txtLastName.DataBindings.Add( "Text", Customer, "LastName" );

}

private void cmdOK_Click(object sender, System.EventArgs e)
{
this.Close();
}

}
}


***********
END OF SOURCE FILES
***********
 
G

Guest

Hi Sijin,

Here is the new code in Form1 with the binding manager code you have
suggested. It does not make any difference. I'm doing it correctly?

private void cmdOpenModalForm_Click(object sender, System.EventArgs e)
{
BindingManagerBase bindingManagerBase = this.BindingContext[Customer];
bindingManagerBase.SuspendBinding();

modalForm form = new modalForm();
form.Customer = Customer;
form.ShowDialog();

bindingManagerBase.ResumeBinding();
}


Thanks you

Sylvain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top