Friday, May 17, 2013

How to passing the value from One form to another form in windows application c#

 Write code on Form -1 :--
=======================

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 objForm2 = new Form2(this);
            objForm2.Show();

        }

      public void PassValue(string strValue)
    {
        textBox1.Text = strValue;
    }


    }
}
------------------------------------------------------------------------------------------------------------


Write code on form : 2:-
=====================
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 WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Form1 ownerForm = null;
        public Form2()
        {
            InitializeComponent();
        }
        public Form2(Form1 ownerForm)            //create parameterized  constructor for form 1
        {
            InitializeComponent();
            this.ownerForm = ownerForm;
        }
        private void Form2_Load(object sender, EventArgs e)
        {
        
        }

      

        private void button1_Click(object sender, EventArgs e)
        {
            this.ownerForm.PassValue(textBox1.Text);
            this.Close();
        }
    }
}