Passing data between forms

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • olsongt
    New Member
    • May 2007
    • 4

    Passing data between forms

    I have a C# problem I've been struggling with for a week now. I can't figure it out. I've made a sample application to demonstrate my problem: how do I pass data between forms? I've read tons of posts/articles etc. about doing this, but I cant make it work. The application I'm writing has a bunch of forms, so when I'm done with one, I'd like to close it to free resources. On the close button event on these forms, I'd like to update variables I've declared but not displayed in the main form class. (I'd really like to send back an array of variables from these forms I think - wow, that sounds daunting for me). Anyway, I can't seem to make it work. Here's my example:

    Form1 code:
    Code:
    public partial class Form1 : Form
        {
            private string f2TB1;
            private string f2TB2;
            public Form1()
            {
                InitializeComponent();
            }
            public string frm2TextBox1
            {
                set
                {
                    f2TB1 = value;
                }
            }
            public string frm2TextBox2
            {
                set
                {
                    f2TB2 = value;
                }
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 newFormTwo = new Form2();
                newFormTwo.ShowDialog();
            }
            private void button2_Click(object sender, EventArgs e) //display values
            {
                string prompt = string.Format("Form1:{0}, Form2: {1}, {2}", textBox1.Text, f2TB1, f2TB2);
                MessageBox.Show(prompt);
            }
        }
    Form2 code:
    Code:
    public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form1 frm1 = new Form1(); //This works to create a new Form1 
                                                        //and keep the old text I typed into its
                                                        // textBox1 intact after I close Form2
                                                       //?why would it since I created a new Form1
                frm1.frm2TextBox1 = textBox1.Text;
                frm1.frm2TextBox2 = textBox2.Text;
                this.Close();
            }
        }
    When I click on the button in Form1 to MessageBox display the values I've typed into the textBoxes on forms 1 and 2, the Form1 textBox1 value is correct, but the Form2 textBox1 and 2 values (f2TB1 and f2TB2) are null. Why won't this work? Arg!!

    Is there a better way to pass the values? Maybe make invisible textBox controls on Form1 and update them when I close Form2 - seems cheesy?

    Thanks in advance for any help.
  • olsongt
    New Member
    • May 2007
    • 4

    #2
    Okay, after messing with the code more, I realized that I created a new Form1 that wasn't visible and that instance of Form1 has the correctly updated variables f2TB1 and 2. Well, I really didn't want to create another instance of Form1, I just want to pass the data from textBoxes back to the existing instance of Form1.

    I'll keep struggling along. Any thoughts would be appreciated.

    Comment

    • olsongt
      New Member
      • May 2007
      • 4

      #3
      Hmmm . . . .Delegates?

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by olsongt
        Hmmm . . . .Delegates?
        Check out this .NET article on Sessions about How to pass information between web pages

        There are other means to pass information between forums too: use cookies, use the view state, use hidden fields.

        I find that sessions are the easiest.

        -Frinny

        Comment

        • mwalts
          New Member
          • May 2007
          • 38

          #5
          Originally posted by olsongt
          I have a C# problem I've been struggling with for a week now. I can't figure it out. I've made a sample application to demonstrate my problem: how do I pass data between forms? I've read tons of posts/articles etc. about doing this, but I cant make it work. The application I'm writing has a bunch of forms, so when I'm done with one, I'd like to close it to free resources. On the close button event on these forms, I'd like to update variables I've declared but not displayed in the main form class. (I'd really like to send back an array of variables from these forms I think - wow, that sounds daunting for me). Anyway, I can't seem to make it work. Here's my example:

          Form1 code:
          Code:
          public partial class Form1 : Form
              {
                  private string f2TB1;
                  private string f2TB2;
                  public Form1()
                  {
                      InitializeComponent();
                  }
                  public string frm2TextBox1
                  {
                      set
                      {
                          f2TB1 = value;
                      }
                  }
                  public string frm2TextBox2
                  {
                      set
                      {
                          f2TB2 = value;
                      }
                  }
                  private void button1_Click(object sender, EventArgs e)
                  {
                      Form2 newFormTwo = new Form2();
                      newFormTwo.ShowDialog();
                  }
                  private void button2_Click(object sender, EventArgs e) //display values
                  {
                      string prompt = string.Format("Form1:{0}, Form2: {1}, {2}", textBox1.Text, f2TB1, f2TB2);
                      MessageBox.Show(prompt);
                  }
              }
          Form2 code:
          Code:
          public partial class Form2 : Form
              {
                  public Form2()
                  {
                      InitializeComponent();
                  }
          
                  private void button1_Click(object sender, EventArgs e)
                  {
                      Form1 frm1 = new Form1(); //This works to create a new Form1 
                                                              //and keep the old text I typed into its
                                                              // textBox1 intact after I close Form2
                                                             //?why would it since I created a new Form1
                      frm1.frm2TextBox1 = textBox1.Text;
                      frm1.frm2TextBox2 = textBox2.Text;
                      this.Close();
                  }
              }
          When I click on the button in Form1 to MessageBox display the values I've typed into the textBoxes on forms 1 and 2, the Form1 textBox1 value is correct, but the Form2 textBox1 and 2 values (f2TB1 and f2TB2) are null. Why won't this work? Arg!!

          Is there a better way to pass the values? Maybe make invisible textBox controls on Form1 and update them when I close Form2 - seems cheesy?

          Thanks in advance for any help.
          Seems to me like he's using Windows forms not Web forms.

          In which case, there is a lot of things you could do.

          For example, since your using ShowDialog() you could make the information you want to pass public properties and then set them as the form exits. These values will then be available to the calling form after the ShowDialog call.

          Alternatively, you could pass a reference to the main form in the constructor of the other forms, then use that reference as you exit to set the main forms public properties

          That is,
          Form1 code:
          Code:
          public partial class Form1 : Form
              {
                  private string f2TB1;
                  private string f2TB2;
                  public Form1()
                  {
                      InitializeComponent();
                  }
                  public string frm2TextBox1
                  {
                      set
                      {
                          f2TB1 = value;
                      }
                  }
                  public string frm2TextBox2
                  {
                      set
                      {
                          f2TB2 = value;
                      }
                  }
                  private void button1_Click(object sender, EventArgs e)
                  {
                      Form2 newFormTwo = new Form2(this);
                      newFormTwo.ShowDialog();
                  }
                  private void button2_Click(object sender, EventArgs e) //display values
                  {
                      string prompt = string.Format("Form1:{0}, Form2: {1}, {2}", textBox1.Text, f2TB1, f2TB2);
                      MessageBox.Show(prompt);
                  }
              }
          Form2 code:
          Code:
          public partial class Form2 : Form
              {
                  Form1 frm1;
                  public Form2(Form1 frm1)
                  {
                      this.frm1 = frm1;
                      InitializeComponent();
                  }
          
                  private void button1_Click(object sender, EventArgs e)
                  {
                      frm1.frm2TextBox1 = textBox1.Text;
                      frm1.frm2TextBox2 = textBox2.Text;
                      this.Close();
                  }
              }
          Hope that helps

          Comment

          • olsongt
            New Member
            • May 2007
            • 4

            #6
            Originally posted by mwalts
            Alternatively, you could pass a reference to the main form in the constructor of the other forms, then use that reference as you exit to set the main forms public properties
            That's what I thought I was doing until I realized that to pass the references in a constructor, I needed to create a new instance of the Form1 class which meant I had two Form1s open. I couldn't figure out how to reference the "already open" instance of Form1. I still can't really.

            Comment

            • mwalts
              New Member
              • May 2007
              • 38

              #7
              Originally posted by olsongt
              That's what I thought I was doing until I realized that to pass the references in a constructor, I needed to create a new instance of the Form1 class which meant I had two Form1s open. I couldn't figure out how to reference the "already open" instance of Form1. I still can't really.
              If you open form 2 from form one, just do it like I did in my example. Pass the keyword "this" to the constructor of the second form and that will give you a reference to the already open Form1.

              If your opening Form2 from some other form somewhere and you want to reference the open instance of Form1, then you could create a static global property of the current istance of Form1, which you update when Form1 is loaded, then use that static property whenever you need to communicate with the open form.

              Not sure if that's the best practice, but I know it works.

              Good luck,

              -mwalts

              Comment

              Working...