0

Can anyone help me with the following problem? : I have two forms in my winform application. In form1, i have a button to open form2 with. In form2, i have a Listbox getting his items from XML:

private void FillListBox()
{
    string filename = @"employee.xml";
    XmlDocument doc = new XmlDocument();
    doc.Load(filename);
    XmlNodeList xnList = doc.SelectNodes("/Information/username");
    foreach (XmlNode xn in xnList)
    {
        ListboxUsername.Items.Add(xn.InnerText);
    }
}

in form1, i have a combobox also getting his items from the same XML file:

public void FillCombobox()
{
    comboboxPersonen.Items.Clear();
    string filename = @"emplyee.xml";
    XmlDocument doc = new XmlDocument();
    doc.Load(filename);
    XmlNodeList xnList = doc.SelectNodes("/Information/username");
    foreach (XmlNode xn in xnList)
    {
        comboboxUsername.Items.Add(xn.InnerText);
    }
}

In form2 i'm able to edit the xmlnodelist in the listboxUsername. And when closing form2, i want the most recent items in the comboboxUsername. Something like a postback in asp.net, but then in winforms application. Any suggestions?

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
    //acces comboboxusername in form1 to update it with recent xml list items        
}

thanks for your time.

Andy
  • 3,997
  • 2
  • 19
  • 39
user3114347
  • 349
  • 1
  • 5
  • 19
  • Are you persisting the edited xml back to the file before closing Form2? If you are you could fire an event from Form2 that Form1 can handle in which you would have Form1's combobox reload from the file. If not you could pass the updated XmlNodeList in a custom EventArg and have Form1 reload the combobox from the passed XmlNodeList. – Kevin Oct 20 '15 at 12:22
  • yes editing hapens before closing Form2. what kind of event should i fire and how to recieve it in Form1? – user3114347 Oct 20 '15 at 12:26
  • You might consider looking at http://stackoverflow.com/questions/31190976/how-do-i-update-a-child-form-from-another-child-form/31191633#31191633 and also browsing the answers around mine. The overall answer is "maybe you don't want to do this directly because a centralized approach might be necessary", but we also all touch on how to do it more directly. Below, Dan might be what you need too, but you might have missed the fact that some items might be nested in a parent container (or be multiple layers deep). You might need to check if the items you iterate through have child container items. – Palu Macil Oct 20 '15 at 14:04
  • On second glance, Dan's answer is incorrect. You can't access the controls as a field of the dialog itself (unless you've written it that way on purpose). You'd need to use the controls collection. The main thing you need to understand is that things might be nested in controls inside there rather than in one flat collection. Other answers: http://stackoverflow.com/questions/15186828/loop-through-all-controls-on-a-form-even-those-in-groupboxes and http://stackoverflow.com/questions/3419159/how-to-get-all-child-controls-of-a-windows-forms-form-of-a-specific-type-button – Palu Macil Oct 20 '15 at 15:24

3 Answers3

1

If you open Form2 as a modal dialog, you can access the form after it has been dismissed, like this:

private void cmdOpenForm2_Click( object sender, EventArgs e )
{
  using ( var dlg = new Form2() )
  {
    if ( dlg.ShowDialog(parent) == DialogResult.OK )
    {
      // do stuff with dlg:
      foreach(var itm in dlg.ListboxUsername.Items )
        blah blah
    }
  }
}
Dan Byström
  • 9,067
  • 5
  • 38
  • 68
0

You can get open forms using

Application.OpenForms

You can get the required form using:

foreach(Form f in Application.OpenForms)
{
    if (f.Name == "Form2")
        {
            //Do Stuff..
        }
    }
}
Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52
  • private void Form2_FormClosing(object sender, FormClosingEventArgs e) { } this is my closing eventhandler. How do i acces comboboxUsername with Openforms? – user3114347 Oct 20 '15 at 12:15
  • It'll be in the Controls collection if you use this method to find the other form. – Palu Macil Oct 20 '15 at 15:31
0

Use callback event from Form1.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Open Form2
        Form2 f = new Form2();
        // Set callback
        f.SetCallbackFromForm1(FillComboBox);
        f.ShowDialog(this);
    }

    private void FillComboBox()
    {
        comboBox1.Items.Add("Sample1");
        comboBox1.Items.Add("Sample2");
        comboBox1.Items.Add("Sample3");
    }
}

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        FormClosed += Form2_FormClosed;
    }

    // This is callback var
    private Action callbackFromForm1;

    // Set callback
    public void SetCallbackFromForm1(Action callback)
    {
        callbackFromForm1 = callback;
    }

    void Form2_FormClosed(object sender, FormClosedEventArgs e)
    {
        // When form closed, our callback will be called.
        callbackFromForm1();   
    }
}

If you need to transmit some object(data) from form2 to form1 when it closing, you can create callback with necessary parameters.

f14shm4n
  • 441
  • 3
  • 12