how to get value from one form to another form and vice versa

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arvindnitin7
    New Member
    • Feb 2013
    • 2

    how to get value from one form to another form and vice versa

    I have two form in my project 1st form is FORM1 and 2nd form is FORM2.
    FORM1 contain 3 textbox,1 button and FORM2 also contain 3 textbox, 1 button. I want to do....
    when user fill FORM1 textboex and click button1 then all entries should be shown on FORM2. and vice versa plz help me.i used this code its working very good. when i pass value in form2 textboxes its show on form1 textbox. but i want to do, if textbox of form1 is already fill then form2 textboex should also shws form1 textbox value. plz help me
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace punchout
    {
        public partial class Form1 : Form
        {
            public string RichText { get { return richTextBox1.Text; } set { richTextBox1.Text = value; } }
            public string txtbox1 { get { return textBox1.Text; } set { textBox1.Text = value; } }
            public string txtbox2 { get { return textBox2.Text; } set { textBox2.Text = value; } }
            public string txtbox3 { get { return textBox3.Text; } set { textBox3.Text = value; } }
            public Form1()
            {
                InitializeComponent();
            }
    
            private void richTextBox1_Click(object sender, EventArgs e)
            {
                var form2 = new Form2(this);
                form2.Show();
            }
        }
    }
    
    
    
    
    
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace punchout
    {
        public partial class Form2 : Form
        {
            private Form1 parentForm;
            
            public Form2(Form1 form)
            {
                InitializeComponent();
                this.parentForm = form;
            }
    
            private void Form2_Load(object sender, EventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //if (this.parentForm == null)
                //{
                   // return;
                //}
                parentForm.RichText = textBox1.Text + Environment.NewLine + textBox2.Text + Environment.NewLine + textBox3.Text;
                parentForm.txtbox1 = textBox1.Text;
                parentForm.txtbox2 = textBox2.Text;
                parentForm.txtbox3 = textBox3.Text;
                this.Close();
            }
        }
    }
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    Please re-word that last paragraph.
    It is not an easy read and I fear that you will not get the correct answer with the way it is worded. After two readings, even I am not sure exactly what you are asking for the code to do.

    Comment

    • gvuksa
      New Member
      • Feb 2013
      • 7

      #3
      Code:
      private void richTextBox1_Click(object sender, EventArgs e)
               {
                   var form2 = new Form2(this);
      
                   form2.textBox1.Text = this.textBox1.Text
      
                   form2.Show();
               }

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        I think you mis-understand, not the code, the text of the question.

        Comment

        Working...