How to pass values from 1 form to another in c# windows?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raghumrao
    New Member
    • Jul 2010
    • 1

    How to pass values from 1 form to another in c# windows?

    How to pass values from 1 form to another in c# windows?
  • jay123
    New Member
    • Sep 2008
    • 121

    #2
    This may help

    There are two common ways to pass data from one page to another, using http Get and Post methods. In Get method, data passed to server using URL encoding. So with Get method data added in URL as qu…

    Comment

    • Sfreak
      New Member
      • Mar 2010
      • 64

      #3
      There are many ways to do that. Here are some examples:

      You can declare a public static method on the first form that sets a static class attribute and call the method from the second form. Something like this:

      ##### Form that receive values

      Code:
      public partial class Form2 : Form
          {
              static string name { get; set; }
              public Form2()
              {
                  InitializeComponent();
              }
              public static void setName(string pName)
              {
                  name = pName;
              }
          }
      ##### Form that send values
      
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  Form2.setName("Sfreak");
              }
          }
      Or you pass the instance of the form that will receive the values to the one that will send values to control it using methods with referenced parameters.

      Better than this you can implement an Observer pattern that manages the first form using an Interface between them.

      Comment

      Working...