Pass Data between two Winforms

  1. Create a Winform Application
  2. Create 2 Forms Form1,Form2
  3.  Initialize Form2 in Form1
    1. Form2 f2 = new Form2();
    2. f2.ShowDialog();
    3. Create a Property Name in Form2 Constructor Assign some value
    4. you can get the property value using f2.Name in Form1//Form2

      public partial class Form2 : Form
      {
      public string Name { get; set; }
      public Form2()
      {
      InitializeComponent();
      Name = “DoItDotnet”;
      }
      }

      //Form1

      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeComponent();
      Form2 f2 = new Form2();

      MessageBox.Show(f2.Name.ToString());

      //This will just display the Form Name in MessageBox We need to access the properties inside the Form2 for desired result

      MessageBox.Show(f2);

      }
      }

 

Leave a comment