0

I have a parent form and a dialog. I need to pass info from the parent to the dialog

Here's what I have:

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;
  }

In DlgGraphOptions(child/dialog) form, I have intialitzed

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

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

So I need to pass theDGroup.m_SerOpts from parent to the dialog,so I have done

_frmDlgGraphOptions.m_SerOpts = theDGroup.m_SerOpts;

in the parent. Now in the child:

public GraphOpts_t m_SerOpts = new GraphOpts_t;

This seems to be wrong. I don't want to be reinitializing it.

Joe White
  • 94,807
  • 60
  • 220
  • 330
user575219
  • 2,346
  • 15
  • 54
  • 105
  • duplicate of: http://stackoverflow.com/questions/280579/how-do-i-pass-a-value-from-a-child-back-to-the-parent-form – Tremmors Jun 01 '12 at 21:41

2 Answers2

1

I think you should change your code in this way:

First, in the DlgGraphOptions form, change the constructor of 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);
}

then create a public property with only the getter returning the internal GraphOpts

public GraphOpts_t Options 
{ 
    get{ return m_SerOpts; }
}

then, in the calling form, change uour code

// 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;  
}  

This approach will force the user of your dialog to pass an initialization value or null. However your InitSettings will work with a initialized value and you don't have initialized two times your options instance.
(Actually there isn't a big improvement from your code, but I think it is a better approach)

Steve
  • 213,761
  • 22
  • 232
  • 286
0

Your child class should probably have the m_SerOpts as a property:

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

Your click event can probably be cleaned up like this:

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;
    }
  }
}

where in your DlgGraphOptions form, you need to set the form's DialogResult property in the OK or Save button event.

You could also just pass m_SerOpts object through the constructor:

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

  public DlgGraphOptions(GraphOpts_t serOpts) {
    InitializeComponents();
    m_SerOpts = serOpts;
  }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • I need it during initialization. It would be good if I could include it in the constructor – user575219 Jun 01 '12 at 22:04
  • @user575219 Not sure I follow. The object looks like it's already initialized in `theDGroup.m_SerOpts`. You could always just check for null and initialize then if that's the issue. – LarsTech Jun 01 '12 at 22:06