I believe the most elegent solution is to pass values using event notification. I found this solution on another site and tried it to pass data between MDI child and parent -- it works fine.1. In the child form (the form that you want to send information from), first create the event handler: public event EventHandler NotifyParent; 2. Also in the child form, create the event handler code. Note that here we are sending the value of "textBox1.text" as data when we publish this event. protected void OnNotifyParent() { NotifyParent(textBox1.text, EventArgs.Empty); } 3. The final task in the child form is to create the code to actually fire this event we have created. Here we are firing the event when a button is clicked: private void btnOK_Click(object sender, EventArgs e) { this.OnNotifyParent(); }
4. Next we need to add code to the MDI parent window to subscribe to this new event that the MDI child window will be firing when the "OK" button is clicked. First we wire up the event by adding the EventHandler to the code that creates and shows the child form: private void addChildForm() { childForm = new childForm(); childForm.MdiParent = this; childForm.NotifyParent += new EventHandler(childForm_NotifyParent); //<=== add this EventHandler childForm.Show(); } 5. Finally, we add a method to process the event when it is received by the parent form. Here we are taking the text value sent from the child form and displaying it in a text box on the parent form. The "object sender" is the "textBox1.Text" value sent in the "OnNotifyParent" method in the child form above: private void childForm_NotifyParent(object sender, System.EventArgs e) { txtParentForm.Text = sender.ToString(); } |