How to pass values from child form to parent form

This is regarding window based system in C#.net

Page 1 of 1

2 Replies - 22164 Views - Last Post: 20 March 2009 - 03:38 AM Rate Topic: -----

#1 amitmane555  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-January 09

How to pass values from child form to parent form

Post icon  Posted 20 March 2009 - 12:55 AM

How to pass values from child form to parent form
Is This A Good Question/Topic? 0
  • +

#5 searockruz  Icon User is offline

  • D.I.C Regular

Reputation: 24
  • View blog
  • Posts: 460
  • Joined: 07-March 09

Re: How to pass values from child form to parent form

Posted 20 March 2009 - 03:11 AM

you can pass values between two forms using public shared variables

Lets assume parent from as Form1.cs and child form as Form2.cs


 Form1.cs
public partial class Form1 : Form
	{



		public static String fff = ""; // declared static so that the values remains same between diff objects

		public  string a
		{
			set
			{
			 fff = value; 
			}
		}
		Form2 frm = new Form2();
		
		 public Form1()//constructor
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (frm.IsDisposed)
			{

			}
			else
			{
				frm.Show(); //show child form
			}
			
		}

		private void button2_Click(object sender, EventArgs e)
		{
		   
				MessageBox.Show(fff);//check for the value
		   
	   
		}

		private void Form1_Load(object sender, EventArgs e)
		{

		}
	}



Form2.cs
public partial class Form2 : Form
	{
		string b = "";
		public Form2()
		{
			InitializeComponent();
			
		}

		private void Form2_Load(object sender, EventArgs e)
		{
			Form1 frm = new Form1();//creating object of parent to access shared variable
			//do not use .show method as we dont want to display the form
			frm.a = "sdfsdf";// passing value
		}

		private void Form2_FormClosing(object sender, FormClosingEventArgs e)
		{
		   
		}

		
	}


Was This Post Helpful? 1

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: How to pass values from child form to parent form

Posted 20 March 2009 - 03:38 AM

View Postamitmane555, on 19 Mar, 2009 - 11:55 PM, said:

How to pass values from child form to parent form


See CodeProject: Passing Values between Forms
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1