Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 C# C#4.0 Homework
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 21-Sep-12 21:26pm
Edited 24-Sep-12 23:37pm
v3
Comments
Wes Aday at 22-Sep-12 6:59am
   
How exactly is this any kind of a problem?
Rajesh Anuhya at 25-Sep-12 4:19am
   
we will not do any Homework here,
-RA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
This is for Form2
 
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:
 
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
  Permalink  
v3
Comments
vasanthkumarmk at 22-Sep-12 4:44am
   
Value is not getting from the first form.
Ragamayura at 24-Sep-12 7:58am
   
Put the Button Click code of form1 into form2
Member 8083455 at 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....?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

in Form1.cs file add following code...
 
public string val { get; set; }
 
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...
 
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...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
You have to declare
public
variable in your second form, then from form1's button click event, write following code
 
protected void btn_sub1_Click()
{
Form2 f2=new Form2();
f2.var=txtbx1.Text;
f2.Show();
}
 
in form2 you have to declare public variable like
 
Public string val=string.Empty;
 
Then on form_load event of form2 fetch value from public variable like
 
txtbx2.Text=val;
  Permalink  
Comments
vasanthkumarmk at 22-Sep-12 5:03am
   
Sorry it shows the error!!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

First Go Soluntion Explorer to Program.cs there set Your Form Which one you want to appear first like login form
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100