Windows窗体 - 传递对象到模态窗体来填充,然后传回

Windows窗体 - 传递对象到模态窗体来填充,然后传回

问题描述:

我有一个使用Dictionary作为其数据源的UltraGrid。我想将字典传递给另一个(模态)窗体以进行操作,并将更改反映到父窗体上的字典中。Windows窗体 - 传递对象到模态窗体来填充,然后传回

我能够将字典传递给孩子的形式,放屁到我的心中高兴,但没有任何变化反映在父表单上的字典。我相信这是因为子窗体上的字典参数没有引用同一个对象。

我真的不想在参考字典中传递。模式形式有一个私有构造函数和一个公共静态方法ShowForm()。我不使用它的实例。有人能给我一个骨头吗?

+0

你能证明你的一些代码?打开子表单并传递字典的部分。 – Steve 2012-07-20 21:38:43

+0

谢谢你的回应,史蒂夫。我能够得到它的工作,并在下面发布解决方案。 – lintmouse 2012-07-23 14:48:51

好吧,我能得到这个做两件事情的工作:

1)确保该字典将它传递给孩子的形式,而不是初始化一个空的字典子窗体之前进行初始化。

2)当子窗体关闭时,将字典指定回网格上的数据源。

这里是父窗体的代码显示它在行动:

private void addColorCodeLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) 
    { 
     var assignedColorCodes = 
      (Dictionary<string, string>)this.subtypeColorCodesUltraGrid.DataSource; 

     //Initialize a null dictionary so that SubtypeColorCodeForm will reference the same dictionary. 
     if (assignedColorCodes == null) 
      assignedColorCodes = new Dictionary<string, string>(); 

     SubtypeColorCodeForm.ShowForm(this, new ImageServerProxy(this.tbImagingUri.Text), 
      assignedColorCodes); 

     //Assign the updated dictionary back to the data source. 
     this.subtypeColorCodesUltraGrid.DataSource = assignedColorCodes; 
    }