将自定义控件放在不同的WinForms中时保持对象引用

时间:2016-07-22 21:30:09

标签: c# winforms user-controls

嗨专家,

考虑到MVC模式我在这个控件中创建了一个用户控件( MyUserControl1 ),我使用了Windows标准控件并将其绑定到我的数据类,如grid.bind,textbox。绑定等,并将此 MyUserControl1 放置在winform( FORM1 )上,它可以正常工作但当我放置另一个表单上的 MyUserControl1 FORM2 )并通过 FORM1 进行任何活动它会自动反映在 FORM2 上,反之亦然。 (参考快照)

似乎控件在两种形式中都具有相同的引用或链接,而使用不同的名称放置 MyUserControl1

似乎MyUserControl在两种形式都有相同的引用但我想要的是 MyUserControl1 应该像独立的那样工作,例如表格上的任何变更都不应反映其他形式。

有人可以帮我解决这个问题吗?

谢谢! enter image description here

相关代码:

FORM 1 CODE

private void COASegment4CompanyFrm_Load(object sender, EventArgs e)
{
     //placed this MyUserControl form and passing role to get role based data.*
     MyUserControl1.userRole = UserRole.Company
}

FORM 2 CODE

private void COASegment4MainFrm_Load(object sender, EventArgs e)
{
     //placed this MyUserControl form and passing role to get role based data.
     MyUserControl1.userRole = UserRole.Main
}

MyUserControl代码:

private List<MyTypeClass> MyDataList;
private void COASegment_CTL_Load(object sender, EventArgs e)
{
     MyDataList = COASegment_BLCFactory<COASegment_CTL>.ClassFactory(this, userRole);
     // This ClassFactory class return List object using as data source. 
     // return different data based on role but all columns are same
     // e.g. userRole=Company data could be id, name, date etc, for the company and when pass userRole=Main so data would be different but same columns id, name, date etc.

     //Data Binding
     txtCode.DataBindings.Add(new Binding("Text", MyDataList, "code"));
     txtName.DataBindings.Add(new Binding("Text", MyDataList, "name"));

}

1 个答案:

答案 0 :(得分:0)

我得到了解决方案, 数据源变量类型是静态的