Click here to Skip to main content
15,905,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody,

Please give me any code, example or link, that I can pass values
from a windows form to another.


Thanks in advance.

Att:

Leonardo Ayala R.
Posted

You will find my solution and other solutions in this discussion:
How to copy all the items between listboxes in two forms[^].

A note: there is no "child window" and "parent window" in case those windows are forms. There are "owned form" and the "owner form". You need to use this relationship if your need UI integrity.

—SA
 
Share this answer
 
Comments
Tarun.K.S 21-Apr-11 14:30pm    
Aaha elegant solution together with a good piece of advice as always. :) 5d!
Sergey Alexandrovich Kryukov 21-Apr-11 14:37pm    
Thank you, Tarun.
--SA
leocode7 21-Apr-11 16:55pm    
Thank, this is good!!!
Sergey Alexandrovich Kryukov 21-Apr-11 17:37pm    
Great. You're welcome.

Thanks for accepting this answer.
Good luck, call again.
--SA
In your child form, declare a publicly visible variable. Then from the parent form, access it by child.variable.

public class childform
{
public int variable;
}

in parent, you can read/pass the value by
child = new childform();
child.variable = 1;

You may also wish to declare it as
static public int variable
if you do not wish to create an instance of the childform first.
 
Share this answer
 
Comments
leocode7 21-Apr-11 13:53pm    
But I want: when I close the child-window then I pass the content in my textbox to parent-window.
what method i need to call???

Please give me a code.

Thanks.
leocode7 21-Apr-11 13:55pm    
I give you the link of image that make it in Fox 9.

http://img534.imageshack.us/img534/5787/modelchilwindow.jpg

I want do this in C#

Thanks again. :)
You could create a Class that holds the values you want to store. Pass the Class as a parameter to your child form.
C#
MyClass cls = new MyClass();
MyForm frm = new MyForm(cls);
frm.Show(); // Or ShowDialog();
string something = cls.Value; // Although it probably won't have a value here yet...


All of this is of course pretty basic .NET stuff... You should consider reading some books and/or articles[^] :thumbsup:
 
Share this answer
 
v2
on the child form add a button and on the click event if certain conditions are met use
C#
string something = "some value"; this.DialogResult=DialogResult.OK;

and if the conditions are not met then
C#
this.DialogResult=DialogResult.Cancel;


Now in the parent form set if the child form's DialogResult is DialogResult.OK then set child form's string to textBox field on parent form like this.
C#
ChildForm childform = new ChildForm();                                                   if (cildform.ShowDialog() == DialogResult.Cancel)                                      return;                                                       textBox1.Text=childform.something; 
 
Share this answer
 
v6

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