Click here to Skip to main content
15,922,155 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi frnds,

am working on c# project using SqlDatabase.

I have two forms Form1 and form2.

In form1 i have 2 textboxes and button
In form2 I have 2 labels

I want to pass Form1 textboxes values to form2 labels.

Please can you help me

Thanks in advance.
Posted

 
Share this answer
 
Comments
Member239258 1-Jan-15 3:46am    
sir, how to pass multiple textbox values.

my code...
Form2 frm = new Form2(TxtName.Text);
frm.Show();

I have another textbox with TxtEmail

Please help.
Abhishek Pant 1-Jan-15 3:51am    
When you pass please fetch values like this in a constructor as

public Form2(string strTextBox, string TxtEmail, string Txtaddress)
{
InitializeComponent();
label1.Text=strTextBox;
label2.text=TxtEmail;
label3.text=Txtaddress;
}
why didn't you google it??

it's the basic question, i think when you are able to post here then you must be able to search it on google before posting here..
Few links here..

Passing Data Between Forms[^]
Pass value between forms using events[^]
Pass value between forms using events[^]
 
Share this answer
 
As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA
 
Share this answer
 
Hi,

There are two ways:
1. Use the constructor to take the value as input.
2. Use the property.
 
Share this answer
 
Take a static class Property.
C#
class Class1
   {
      static string name;
     static  string add;

       public static string PName
       {
           get
           {
               return name;
           }
           set
           {
               name = value;
           }
       }

       public static string PAddress
       {
           get
           {
               return add;
           }
           set
           {
               add = value;
           }
       }
   }

Now on Form1 take two textbox and two button
and write this code on .cs file.
C#
private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "")
            {
                MessageBox.Show("Please don`t leave any textbox empty.");
            }

            else
            {

                 Class1.PName = textBox1.Text;
                 Class1.PAddress = textBox2.Text;

                Form2 f2 = new Form2();
                f2.Show();

            }
        }

Now take another form (Form2)
Take two label and one button on it.
Now write this code on .cs file to received value from Form1 and display it.
C#
private void button1_Click(object sender, EventArgs e)
{
    this.Close();
}

private void Form2_Load(object sender, EventArgs e)
{

    label1.Text = Class1.PName;
    label2.Text = Class1.PAddress;
}
 
Share this answer
 
v2
 
Share this answer
 
v2
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[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—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