I have a MDI application. One of the forms needs to be able to have multiple instances of it open at the same time. Within this app I have a Program class. For each instance of the form I need to place a Program object into each form. This is working, however, everytime data is changed it changes all of the Program objects within all of the multiple instances of the form.
Here is the Program class (very simple class for now):
public class Program
{
string strProgramCode;
public Program()
{ }
public string ProgramCode
{
get { return strProgramCode; }
set { strProgramCode = value; }
}
}
Here is the code for the form:
frmWeeklyIndividualBudgets tfrmWeeklyIndividualBudgets = new frmWeeklyIndividualBudgets();
tfrmWeeklyIndividualBudgets.Program = this.Program;
tfrmWeeklyIndividualBudgets.Text = this.Program.ProgramCode.ToString() + " Weekly Budget";
this.CheckMdiChildren(tfrmWeeklyIndividualBudgets);
Here is the CheckMdiChildren method:
private void CheckMdiChildren(Form form)
{
foreach (Form frm in this.MdiChildren)
{
if (frm.GetType() == form.GetType())
{
if (frm.GetType().ToString() == "IPAMFinancial_Program_Financial_Breakdown.frmWeeklyIndividualBudgets")
{
frmWeeklyIndividualBudgets tfrm = (frmWeeklyIndividualBudgets)frm;
if (tfrm.Program.ProgramCode == this.Program.ProgramCode)
{
frm.Focus();
return;
}
}
else
{
frm.Focus();
return;
}
}
}
form.MdiParent = this;
form.Show();
}
I strongly suspect the problem is that you've got one Program
object, which all the forms refer to. (That's certainly what the code looks like.) Give each form a new Program
instead, when you create the form.
For example:
frmWeeklyIndividualBudgets tfrmWeeklyIndividualBudgets =
new frmWeeklyIndividualBudgets();
// Give the new form a new Program instance
tfrmWeeklyIndividualBudgets.Program = new Program();
tfrmWeeklyIndividualBudgets.Text = this.Program.ProgramCode.ToString()
+ " Weekly Budget";
this.CheckMdiChildren(tfrmWeeklyIndividualBudgets);
If you want the new form to get a Program
based on the existing one, you should implement a Clone
method in Program
, and do:
tfrmWeeklyIndividualBudgets.Program = this.Program.Clone();