Search code examples
c#.netwindowsshowdialog

Saving Data on Parent Window from a Child Window?


I have a Windows application.

The windows application has our XML Library on it.

  public NetspotXMLLibV1 XMLLib;

    public Form1()
    {
        InitializeComponent();
        XMLLib = new XmlLibrary.NetspotXMLLibV1();
    }

It also has a custom control.

That custom control has a button on it that has

SelectWidgitWindow widgit = new SelectWidgitWindow();
widgit.ShowDialog();

when clicking a button, it opens up the new window. I do some stuff on this new window. When I click a button on the new window I want to save stuff on the Form1 Window

How do I access this on my new Window (SelectWidgitWindow ) ?

Ie

form1.XMLLib.Add(ItemForProcessing);

or

Windows(1).XMLLib.Add(ItemForProcessing);

Please help


Solution

  • Use the ShowDialog overload to which you can pass an owner for the new form:

    Form2 f = new Form2();
    f.ShowDialog(this.Parent);
    

    and in Form2:

    ((Form1)Owner).MyProperty = 11;