How to reload a listbox in a parent form [C#]

microkarl

Regular
Joined
Apr 22, 2004
Messages
88
Location
Morristown, NJ
Hi, I have two windows forms written in C#. The parent form can call the child form to open. In the child form, I have logic to add/remove certain nodes in a XML document. After that, I can close the child form. But I also have listbox control in the parent form to display the items from the exact XML document, since I have updated it, I would like to 'reload' the control so it will display the most updated list. But how do I do it? I suppose I can fire an event on the childform.Close() event, and use this.ParentForm to do it? But I could not see any functions in the parent form.

please help.
 
If the child form is called using the ShowDialog() method then the code will resume in the parent form directly after the ShowDialog() line and you can just call a ReloadXML() method from there. Of course this assumes you don't use the parent form before the child form is closed.

If you need to allow both forms to be used concurrently then one way of doing it would be to pass a reference to the parent form into the child forms constructor.

C#:
public class ParentForm : System.Windows.Forms.Form
{
    public ParentForm()
    {
    }

    private void CreateChildForm()
    {
        ChildForm myChild = new ChildForm(this);
    }

    public void ReloadXML()
    {
        // reload the xml and populate the listbox
    }
}

public class ChildForm : System.Windows.Forms.Form
{
    private ParentForm _parent;

    public ChildForm(ParentForm parent)
    {
        _parent = parent;
    }

    private ChangeXML()
    {
        // alter the xml file
        
        // update the parent
        _parent.ReloadXML();
    }
}
 
Back
Top