Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 use this code but its wrking only one direction, plz help me
C#
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();
        }
    }
}
Posted
Updated 23-Feb-13 3:37am
v2

Don't do it that way!
See here: Transfering information between two forms, Part 2: Child to Parent[^]
Though I suspect you might want to consider this as a form of Transfering information between two forms, Part 3: Child to Child[^]
 
Share this answer
 
Use a separate Public Property for each variable you want to pass between forms or create one Structure to hold all the variables and use a Public Property to pass the Structure.

Form1
* Declare the Properties (Below is an example of a date property)
public System.DateTime Stop_Time {
	get { Stop_Time = m_stop_time; }
	set { m_Stop_Time = value; }
}
System.DateTime m_Stop_Time;

* Set the Properties before instantiating Form2
* Instantiate and Show Form2
* After Form2 closes, use properties to change form fields in Form1

Form2
* In Form Load Event, use Public Properties from Form1 to initialize Form2's controls
* In the Close Event, use Form2's controls to change Form1's Public Properties
 
Share this answer
 
v4

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