We help IT Professionals succeed at work.
Get Started

Send data from form to Sub form

Rama Tito
Rama Tito asked
on
410 Views
Last Modified: 2012-08-14
I have Form1, form2 and Subform2. The code is working but i need to transfer more data from line 82 to Form1.

I got data type in date, int and text. I have to send this parameters from SubForm2 to Form1,
namespace Main_Forms
{
    public partial class Form1 : Form
    {
        Form2 frm2;
        Sub_From.SubForm2 Sfrom;
        public string answer;
        public Form1()
        {
            InitializeComponent();
            
            button2.Enabled = false;
            textBox1.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button2.Enabled = true;
            textBox1.Enabled = true;
            frm2 = new Form2();
            if(frm2.ShowDialog()==DialogResult.Yes)
            {
                Sfrom = new Sub_From.SubForm2();
                Sfrom.FormClosed += new FormClosedEventHandler(SubForm_FormClosed);
                Sfrom.Show(this);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Sfrom != null)
            {
                Sfrom.Close();
                //Sfrom.Dispose();
                Sfrom = null;
            }         
            
        }
        private void SubForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            textBox1.Text = Sfrom.valueans;
            Sfrom.Dispose();
            Sfrom = null;
        }
    }
}
============================================================================
namespace Main_Forms
{
    public partial class Form2 : Form
    {
        //Sub_From.SubForm2 Sform;
        
        public Form2()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Yes;
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
   }
}

===========================================================================
namespace Sub_From
{
    public partial class SubForm2 : Form
    {
       
        public string valueans
        {
            get { return textBox1.Text; }
        }

        public SubForm2()
        {
            InitializeComponent();
        }
       
    }
}

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 3 Answers and 6 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE