Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am opening a form from another one.
Form1 and Form2 two forms are there.

In Form1 i am having a textBox1 and a Button1.
Button1 will open the Form2 using following Code
C#
Form2 f2=new Form2();
f2.Show()

I am not closing or hiding the Form1.
now in form2 I am having another txtValue1 and btn1.
I want to enter some text in txtValue1 and then when I will click on btn1.
the form2 should get closed and the Form1's textBox1 control should contain the value of txtValue1(Form2).


please help. I tried a lot. but I can't.
Posted
Updated 20-Dec-11 10:58am
v2

Mika's answer is perfectly valid and a good one at that.
An alternative, and perhaps what you are looking for, is calling ShowDialog[^] instead of simply Show. This means that the text in your Form2 has to be entered (or user cancels the form) before returning to Form1. This is usually an obvious choice because what purpose would Form2 still have if Form1 is closed?
Anyway, in that case you could put txtValue2 in a Property and read that from Form1, like so:
C#
Form2 f2 = new Form2();
f2.ShowDialog(); // Execution of the code stops here until f2 is closed.
textBox1.Text = f2.MyTextProperty;
f2.Dispose();

Hope that helps :)
 
Share this answer
 
Comments
Wendelius 20-Dec-11 17:19pm    
Yes, this is an alternative if modal approach can be used. 5'd
Sander Rossel 20-Dec-11 17:23pm    
Thanks Mika :)
Sergey Alexandrovich Kryukov 20-Dec-11 18:14pm    
Not clear why disposing f2, you can rather re-use it later. This is a bit of brute force.
I prefer my method which is the most robust from what I know, please see my solution.
For a simple project any one would work though.
--SA
Sander Rossel 21-Dec-11 2:45am    
Disposing Disposable Objects is generally seen as best practice I believe :)
I agree that Form2 might be re-used, but in this example I can't be certain (and actually in my four lines of code it's impossible because f2 goes out of scope as soon as it leaves the method) so I believe disposing it is the best course of action.
Now what you should have said is "why not wrap up the Dispose method in a Try Finally block or use a Using block?" ;)
Amir Mahfoozi 21-Dec-11 0:57am    
+5
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

—SA
 
Share this answer
 
Comments
Amir Mahfoozi 21-Dec-11 0:58am    
+5
Sergey Alexandrovich Kryukov 21-Dec-11 1:03am    
Thank you, Amir.
--SA
Sander Rossel 21-Dec-11 2:50am    
My 5. Interfaces are often the best solution.
Hi,

Try the below coding:

Form1:
C#
private void button1_Click(object sender, EventArgs e)
{
 f2.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
  f2 = new Form2(this);
}


Form2:

C#
Form1 f;
public Form2(Form1 f1)
{
  InitializeComponent();
  f = f1;
}
private void button1_Click(object sender, EventArgs e)
{
  f.Activate();
  int value = Convert.ToInt32(textBox1.Text);
  f.textBox2.Text = value.ToString();
  this.Close();
}
}


Regards,
vasuki.
 
Share this answer
 
v2
Comments
Amir Mahfoozi 21-Dec-11 0:57am    
+5
Sander Rossel 21-Dec-11 2:40am    
The Convert.ToInt32(textBox1.Text) makes no sense at all...
[no name] 21-Dec-11 4:04am    
Thanks for the answer
One (well quite brute-force) way is that you define an event on Form2 with custom eventargs to pass the value of the text box. See the example in: http://msdn.microsoft.com/en-us/library/system.eventargs.aspx[^]. This event would be fired when the button is pressed.

On form1 you would wire that event and place the text in it's textbox.
 
Share this answer
 
Comments
Sander Rossel 20-Dec-11 17:14pm    
My 5 because this is a correct solution. An alternative, though not directly what the op is asking for is perhaps calling ShowDialog instead of Show.
Wendelius 20-Dec-11 17:18pm    
Thanks :)
Sergey Alexandrovich Kryukov 20-Dec-11 18:12pm    
As always, I prefer my interface-based method over the one based on events. Please see my solution.
(I voted 4 for this one.)
--SA
Wendelius 21-Dec-11 0:25am    
Yep, this is more or less a 'brute-force' version. Thanks :) I noticed your post a bit after posting this.
Amir Mahfoozi 21-Dec-11 0:58am    
+5

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