Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am new in C# windows programming and having issues in handling data of windows forms.

Here is the scenario.

I have created one windows form (named as form1) in which i am taking input via various controls such as text boxes, list boxes, combo boxes and trying to show it in another form , till showing one windows form's input value in second form is working fine but when I try to access values of form1 in third form in order to do some computation based on form1 values I am unable to access them.

Please help me in this scenario that how could access the values of form1 in the third form.

Following is the code that how i am doing this

C#
namespace SmallTestApplication
{
   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      private void button1_Click(object sender, EventArgs e)
      {
         previewinput dos = new previewinput();
         dos.Show();
         dos.label2.Text = textBox1.Text;
      }

      public string getdataform3()
      {
         Form3 form3obj = new Form3();
         form3obj.getvar = textBox1.Text;
         return form3obj.getvar;
      }
   }
}

namespace SmallTestApplication
{
   public partial class previewinput : Form
   {
      public previewinput()
      {
         InitializeComponent();
      }

      private void button1_Click(object sender, EventArgs e)
      {
         Form3 objform3 = new Form3();
         objform3.Show();
      }
   }
}

namespace SmallTestApplication
{
   public partial class Form3 : Form
   {
      public Form3()
      {
         InitializeComponent();
      }

      public string getvar;

      private void button1_Click(object sender, EventArgs e)
      {
         Form1 objform1 = new Form1();
         MessageBox.Show(objform1.textBox1.Text)
         //MessageBox.Show(objform1.getdataform3());

         //previewinput dos = new previewinput();
         //MessageBox.Show(dos.label2.Text);
      }
   }
}


Thanks,

Fahad Bin Aziz.
Posted
Updated 22-Mar-12 6:27am
v3

1 solution

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

—SA
 
Share this answer
 

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