SuusvdVen
SuusvdVen

Reputation: 123

Passing a Complete Child Form to another Child Form

I see alot of pages and questions involving passing a value from one form to another. I would like to do something a bit different. Not values, but an entire form, passed as a parameter. Please not this is NOT an MDI form.

I have a parent form with 2 buttons. One button creates a Child1 form among other things. Another (optional) button calls up a different child form editor.

The first button is a validation button which if valid, brings up the Child1 form with a listviewbox filled with key/valuepairs. The second is an editor with it's own buttons. If one of those (submit) buttons are pressed then the first Child1 form should be updated with a new keyvaluepair.

The problem I've been having is that I can't seem to get the Child1 form to reflect the changes. I don't know what the best way is to update this Child1 form. I've tried BeginUpdate with EndUpdate, and refreshing it and now with all the tries, i've gotten a bit lost. I hope someone can help me.

What I actually Want to do is refresh ChildForm1, say it's an inbox, when something gets "saved" or added from the second childform editor. With the button click in this second childform editor the first Childform should refresh.

parent form:

    public ChildForm1 MyboxofValues; 

    private void btn1_Click(object sender, EventArgs e)
    {
            MyboxofValues = new ChildForm1();
            MyboxofValues.Show();
    }

    private void btn2_Click(object sender, EventArgs e)
    {
        Editor myEditor = new Editor();
        myEditor.Show(); 
    }

ChildForm1:

    public ChildForm1()
    {
        InitializeComponent();
        updateMe();

    }
    public void updateMe()
    {
        listView1.Items.Clear();
        if (MainCode.subRows.Count > 0)
        {
            foreach (KeyValuePair<string, string> element in MainCode.subRows)
            {
                ListViewItem lvi = new ListViewItem(element.Value);
                lvi.SubItems.Add(element.Key);
                listView1.Items.Add(lvi);
            }
        }
    }

EditorForm:

    private ChildForm1 originalForm = new ChildForm1();

    private void btnSubmit_Click(object sender, EventArgs e)
    {
            originalForm.updateMe();
    }

I'm sure it's very easy but I can't get it. If someone can help it would be appreciated, save me hours! If there is a better way to update with the submission results that's also fine, please show an example?

For test purposes I have created a "refresh" button for the ChildForm1, which works! (It just calls the updateMe() actually. So it's something wierd I think like that I am creating a new instance of the form and trying to change this nonexisting (null) form instead of the actual ChildForm1. But I want to refresh the listviewBox from out the Editor Form.

Upvotes: 2

Views: 358

Answers (3)

SuusvdVen
SuusvdVen

Reputation: 123

Largely through perceptive help from Servy. I don't know if this might help someone else so here is what worked for me.:

Parent form, MyBox, which is according to how I originally thought it was previously ChildForm1:

  public MyBox()
    {
        InitializeComponent();
        updateMe();
    }
    public void updateMe()
    {
        listView1.Items.Clear();
        if (MainCode.subRows.Count > 0)
        {
            foreach (KeyValuePair<string, string> element in MainCode.subRows)
            {
                ListViewItem lvi= new ListViewItem(element.Value);
                lvi.SubItems.Add(element.Key);
                listView1.Items.Add(lvi);
            }
        }
    }
    private void btnCreateEditor_Click(object sender, EventArgs e)
    { 
        plainTextEditor editor = new plainTextEditor(this);
        Form1.editorOpen = true;
        editor.Show(this);
    }

This Form has a button which calls the Editor Form which looks like this now:

public partial class Editor : Form
{
    private MyBox listviewBoxForm;

    public Editor(listviewBoxForm _form1)
    {
        InitializeComponent();
        inboxForm = _form1;
    }
 }

Actually simple, but didn't see it immediately. I was making it more complicated than it needed to be with delegates etc. This is simple and it seems to work for me.

Upvotes: 0

Servy
Servy

Reputation: 203821

Rather than having Editor be a "child" form of the parent form, make it a child form of ChildForm (aka a grandchild of the parent). Of course since you're not using MDI this is only a logical way of approaching the problem, nothing more.

Rather than having the parent from create the Editor, have the ChildForm create it. When the parent form's second button is clicked have it call a method like CreateEditor of ChildForm. When it creates the editor it can obviously store it for later interaction.

If the parent form needs to do anything else with the Editor it can go through the ChildForm to do it.

Upvotes: 1

Quinton Bernhardt
Quinton Bernhardt

Reputation: 4803

I would suggest not updating one form from another but rather have the working data stored in a object like a view model and always refresh your form from that.

Have a look at the MVP pattern which is useful for WinForms applications.

http://martinfowler.com/eaaDev/ModelViewPresenter.html

Upvotes: 0

Related Questions