Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am working with an Winform application that has more Forms in it.

I want to send and receive data between two forms. Can anyone help???????
Posted

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

—SA
 
Share this answer
 
Comments
Espen Harlinn 1-Jan-12 11:45am    
5'ed!
Sergey Alexandrovich Kryukov 1-Jan-12 12:21pm    
Thank you, Espen.
--SA
In order to do this, one form must know about the existance of the other, which is generally a bad idea unless one opens the other directly.
If Form1 opens Form2, then there are two ways to pass information, depending on whether Form2 is displayed with Show or ShowDialog:

ShowDialog is easy: Create a public property (or several) in Form2, load the values into the property from Form1 before calling ShowDialog and fetch them back as necessary when it returns.

Show is harder - you need to create an event in Form2 which Form1 subsribes to before calling Show - you can then pass data from Form2 to Form1 either via a custom EventArgs, or via properties as above.

In the child form:

C#
public partial class frmChild : Form
   {
   // Signal file change happened
   public event EventHandler Changed;

   protected virtual void OnChanged(EventArgs e)
      {
      EventHandler eh = Changed;
      if (eh != null)
         {
         eh(this, e);
         }
      }

   private void DoSomethingToChangeData()
      {
      OnChanged(null);
      }
   }


----- The asign to eh is in case the handler changes between null check
----- and exec.
----- (unlikely, but possible)

----- The null check is to ensure there is a handler. If not, better to
----- ignore it gracefully, than to rely on the thrown exception
----- (NullReferenceException)

In the Parent form:


C#
public frmParent()
    {
    frmChild.Change += new frmChange.ChangeHandler(Changed);
    }

private void ShowChildForm()
    {
    // Link in his event so if it changes, we detect it.
    frmChild fd = new frmChild();
    fd.ShowDialog();
    }

//
// Fired when the file is changed at a lower level.
//
private void Changed(object sender, EventArgs e)
    {
    }
 
Share this answer
 
Comments
Espen Harlinn 1-Jan-12 11:45am    
5'ed!
You should have a domain level object that can be used via reference in both forms. Forms are there for users to interact with domain level objects. Forms should not "store" data or manipulate data. If you stick to this type of model then any given form can allow the user to interact with the same data any other form can.
 
Share this answer
 
Comments
Espen Harlinn 3-Jan-12 14:57pm    
5'ed! and my wishes for a happy new year Marcus :)
google is your friend, use it before posting your question here. Try these:

Passing Data Between Forms[^]

Passing Data between Windows Forms[^]

hope it helps :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900