Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two forms; form 1 and form 2.

1. I click on a button in form one to open form two.
2. I enter value into a textbox in form two.
3. I click on save in form two.
4. Value is displayed in textbox in form one.

How can I implement this?
Thanks in advance.
Posted
Updated 27-Mar-11 5:14am
v2
Comments
Dalek Dave 27-Mar-11 11:14am    
Edited for Grammar.

Please, do not do what Аslam Iqbal or Manivannan Shan are recommending: it if a bad idea to make any controls on your form public. The reason is simple: when you do that you ties the design of the two forms together - you cannot change form1 without checking that it will not affect form2. You also cannot re-use form2 from form3 because it may not have the same layout or purpose.

Instead, there are two ways to do it:

1) Treat form2 like you would a OpenFileDialog: Show it, wait for the user to press OK and the close form2 and fetch the value in form1. That's easy: Set up a public property which returns the value(s) in the same way the OpenFileDialog supplies FileName
public string Value
   {
   get { return textboxUserInputTo.Text; }
   set { textboxUserInputTo.Text = value; }
Then display your form with
form2 f2 = new form2();
if (f2.ShowDialog() == DialogResult.OK)
   {
   string s = f2.Value;
   ...
   }


2) Set up an event in form2 which says "I have a value for you".
public event EventHandler Changed;

protected virtual void OnChanged(EventArgs e)
   {
   EventHandler eh = Changed;
   if (eh != null)
      {
      eh(this, e);
      }
   }

private void DoSomethingToChangeData()
   {
   OnChanged(null);
   }
Form1 then subscribes to the event.
private void ShowChildForm()
    {
    // Link in his event so if it changes, we detect it.
    form2 f2 = new form2();
    f2.Change += new form2.ChangeHandler(Changed);
    f2.Show();
    }
//
// Fired when the file is changed at a lower level.
//
private void Changed(object sender, EventArgs e)
    {
    form2 f2 = sender as form2;
    string s = f2.Value;
    }
 
Share this answer
 
Comments
Dalek Dave 27-Mar-11 11:16am    
That gets a 5, and you are totally right about Public Form.
It binds them, often unintentionally, I have had this problem before, took ages to figure out what I was doing wrong.
[no name] 27-Mar-11 11:52am    
where do i place the public String value? in form one or form two?
Аslam Iqbal 27-Mar-11 13:37pm    
Inside Form2 class.
Аslam Iqbal 27-Mar-11 13:32pm    
Correct answer.Thanks for finding out my misuse of OOP. my 5
OriginalGriff 27-Mar-11 15:48pm    
It's cool: too easy a mistake to make!
In from2 do the following...(Pass Form1 instance to form2 constuctor and to assign in global.when form2 button click assign value to form1 text box)

Note:Make the textbox control access specifier to public.


C#
public partial class Form2 : Form
   {
       Form1 frm1 = null;
       public Form2(Form form1)
       {
           InitializeComponent();
           frm1 = (Form1)form1;
       }
       private void button1_Click(object sender, EventArgs e)
       {
           frm1.textBox1.Text = "your value";
       }
   }


Like wise, in Form1


C#
private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(this);
            frm2.Show();
        }
 
Share this answer
 
Comments
[no name] 27-Mar-11 8:35am    
i cant seem to access frm1.textbox.text
OriginalGriff 27-Mar-11 10:17am    
Please do not suggest making form controls public: it is against the basic ideas of OOP.
 
Share this answer
 
Set the Form2.Textbox1.modifier to Public. To do this select Form2.Textbox1 and change its modifier property to public.
Rest of the Code is:
Inside form1:
C#
    private void ShowForm2_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.ShowDialog();
  textBox1.Text  = f2.textBox1.Text;
}


Inside Form2:
C#
private void Save_Click(object sender, EventArgs e)
{
    this.Hide();
}
 
Share this answer
 
Comments
OriginalGriff 27-Mar-11 10:17am    
Please do not suggest making form controls public: it is against the basic ideas of OOP.
In addition the the options other Answers provided I want to add my way which I find the most robust: using interfaces. Please see my Answer to this Question: Calling a function from different forms in a project[^].

—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