Click here to Skip to main content
15,850,548 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Homework

I am using windows form with c# code.

I have 2 Forms (named frm1, frm2)
In that frm1 i have one textbox (named txtbx1) and one button (named btn_sub1)
In that frm2 i have one textbox (named txtbx2) and one button (named btn_sub2)

I run the Application the frm1 will load and click the btn_sub1 button, it will opens the frm2, I have entered some values in that txtbx2 textbox and Click the button btn_sub2 that txtbx2 values will comes on the txtbx1 on the frm1


Regards
Vasanth
Posted
Updated 25-Sep-12 0:37am
v3
Comments
[no name] 22-Sep-12 6:59am    
How exactly is this any kind of a problem?
Rajesh Anuhya 25-Sep-12 4:19am    
we will not do any Homework here,
-RA

Hi,
This is for Form2

C#
namespace FormCommunication
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (Form form in Application.OpenForms)
            {
                if (form.Name == "Form1")
                {
                    (form.Controls["textBox1"] as TextBox).Text = textBox1.Text;
                    form.Activate();
                }
            }
        }
    }
}



Now for Form 1:

C#
namespace FormCommunication
{
    public partial class Form1 : Form
    {
        public string Username { get; set; }

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 objSecondForm = new Form2();
            objSecondForm.ShowDialog();
        }
    }
}



Thanks & Regards,
Raghu
 
Share this answer
 
v3
Comments
vasanthkumarmk 22-Sep-12 4:44am    
Value is not getting from the first form.
Raghuveer Kasyap 24-Sep-12 7:58am    
Put the Button Click code of form1 into form2
Member 8083455 25-Sep-12 3:04am    
Thanks for response.

but another problem is that textbox on form1 is on a groupbox.
in such case how to find it....?
in Form1.cs file add following code...

C#
public string val { get; set; }


C#
private void btn_sub1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();

            f2.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            method1();
        }

        public void method1()
        {
            if (val != "")
            {
                txtbx1.Text = val;
            }
        }


On Form2.cs file add following code...

C#
private void btn_sub2_Click(object sender, EventArgs e)
        {
            foreach (Form form in Application.OpenForms)
            {
                if (form.Name.Equals("Form1"))
                {
                    ((Form1)form).val = txtbx2.Text;
                    ((Form1)form).method1();
                }
            }
            this.Close();

        }


Enjoy Coding...
 
Share this answer
 
Hey,

Just try to assign Your txtbx2.text into a global variable when button click.

public static string x;
x=txtbx2.text;

then You can use that global variable to print the text in form 1.

Try it.
 
Share this answer
 
form2

button click event

form1 frm=new form1();
frm.str=textbox1.text;
frm.show();


form1
Declare classlevel variable
public string str="";

load_event

textbox1.text=str;
 
Share this answer
 
Hi,

You have to declare
C#
public
variable in your second form, then from form1's button click event, write following code

C#
protected void btn_sub1_Click()
{
Form2 f2=new Form2();
f2.var=txtbx1.Text;
f2.Show();
}


in form2 you have to declare public variable like

C#
Public string val=string.Empty;


Then on form_load event of form2 fetch value from public variable like

C#
txtbx2.Text=val;
 
Share this answer
 
Comments
vasanthkumarmk 22-Sep-12 5:03am    
Sorry it shows the error!!!
First Go Soluntion Explorer to Program.cs there set Your Form Which one you want to appear first like login form
 
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