Click here to Skip to main content
13,189,506 members (51,859 online)
Rate this:
 
Please Sign up or sign in to vote.
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
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 23-Feb-13 3:35am
Updated 23-Feb-13 3:37am
ProgramFOX205.2K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.171016.2 | Last Updated 23 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100