【问题标题】:Data from parent to dialog从父级到对话框的数据
【发布时间】:2012-06-01 21:37:02
【问题描述】:

我有一个父窗体和一个对话框。我需要将信息从父级传递到对话框

这是我所拥有的:

private void Item_Click(object sender, EventArgs e)
{
  DialogResult result = DialogResult.OK;
  DlgGraphOptions _frmDlgGraphOptions = new DlgGraphOptions();            
  _frmDlgGraphOptions.m_SerOpts = theDGroup.m_SerOpts;
  _frmDlgGraphOptions.ShowDialog(this);
  if (result == DialogResult.OK)
  {
    // Save the revised options to the Data Group
    theDGroup.m_SerOpts = _frmDlgGraphOptions.m_SerOpts;
  }

在 DlgGraphOptions(child/dialog) 表单中,我已经初始化了

public partial class DlgGraphOptions : Form
{
  public GraphOpts_t m_SerOpts = new GraphOpts_t();
}

private void InitSettings(int idxSeries)
{
  m_nMaxPts = m_SerOpts.GetMaxPts(idxSeries);
}

所以我需要将 DGroup.m_SerOpts 从父级传递给对话框,所以我已经完成了

_frmDlgGraphOptions.m_SerOpts = theDGroup.m_SerOpts;

在父级中。现在在孩子身上:

public GraphOpts_t m_SerOpts = new GraphOpts_t;

这似乎是错误的。我不想重新初始化它。

【问题讨论】:

标签: c# winforms


【解决方案1】:

我认为您应该以这种方式更改代码:

首先,在DlgGraphOptions表单中,更改DlgGraphOptions的构造函数

// Force the caller to pass a GraphOpts_t 
// Check if it is a valid instance or create one as new
public partial class DlgGraphOptions(GraphOpts_t input ) : Form 
{ 
     m_SerOpts = (input == null ? new GraphOpts_t() : input);
}

然后创建一个只有 getter 返回内部 GraphOpts 的公共属性

public GraphOpts_t Options 
{ 
    get{ return m_SerOpts; }
}

然后,在调用表单中,更改你的代码

// Pass the m_setOpts from theDGroup 
DlgGraphOptions _frmDlgGraphOptions = new DlgGraphOptions(theDGroup.m_SerOpts);
if(DialogResult.OK == _frmDlgGraphOptions.ShowDialog(this))
{  
    // Save the revised (or new) options to theDGroup  
    theDGroup.m_SerOpts = _frmDlgGraphOptions.Options;  
}  

这种方法将强制对话框的用户传递初始化值或 null。 但是,您的 InitSettings 将使用初始化值,并且您没有初始化两次选项实例。
(其实你的代码并没有太大的改进,但我认为这是一个更好的方法)

【讨论】:

    【解决方案2】:

    您的子类可能应该将m_SerOpts 作为属性:

    public partial class DlgGraphOptions : Form
    {
      public GraphOpts_t m_SerOpts { get; set; }
    }
    

    你的点击事件大概可以这样清理:

    private void Item_Click(object sender, EventArgs e)
    {
      using (DlgGraphOptions _frmDlgGraphOptions = new DlgGraphOptions()) {
        _frmDlgGraphOptions.m_SerOpts = theDGroup.m_SerOpts;
        if (_frmDlgGraphOptions.ShowDialog(this) == DialogResult.OK)
        {
          // Save the revised options to the Data Group
          theDGroup.m_SerOpts = _frmDlgGraphOptions.m_SerOpts;
        }
      }
    }
    

    在您的 DlgGraphOptions 表单中,您需要在 OKSave 按钮事件中设置表单的 DialogResult 属性。

    您也可以只通过构造函数传递 m_SerOpts 对象:

    public partial class DlgGraphOptions : Form
    {
      public GraphOpts_t m_SerOpts { get; }
    
      public DlgGraphOptions(GraphOpts_t serOpts) {
        InitializeComponents();
        m_SerOpts = serOpts;
      }
    }
    

    【讨论】:

    • 我在初始化时需要它。如果我可以将它包含在构造函数中会很好
    • @user575219 不确定我是否关注。该对象看起来已经在theDGroup.m_SerOpts 中初始化。如果这是问题,您总是可以检查 null 并初始化。
    猜你喜欢
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多