Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two forms. a variable VAR is declared in form1, iwant to use its value in form2. how can i do this?
Posted
Comments
Sweety Khan 16-Jun-11 12:38pm    
all answers r nice but still i m unable to code :(
Sweety Khan 16-Jun-11 13:14pm    
o its very simple. go to the properties of textbox, make it public. go in form 2 write
form1 f1 = new form1();
n now textbox value can be access by f1.textbox.text;
thanx everyone for helping :)

First of all you are not allowed to declare variable (using var keyword).
One way is just use a delegate to pass data between forms,
Let the calling form register it as an event and you are done.

Thanks,
Debata
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Jun-11 13:21pm    
Good point, a 5.
--SA
There are a few things here:
1) Which instance of VAR do you want to access - there can be several.
2) Why should Form1 let Form2 access it's variables.

To access something on Form1 from Form2 you need two things: the instance of Form1 that holds the variable you are concerned with, and for Form1 to deliberately expose it so that you can access it. I'll deal with these in reverse order.

The simple way to expose it is to declare it as public:
public string myString = "Hello, I'm a sting in Form1";
But this has problems, and is not recommended because it ties the two forms together - you cannot change the way Form1 works internally without considering the effects on Form2 (and potentially Form3, Form4 and so on). This is against the principles of OOP and is generally thought of as a bad idea.
A better solution is to use a public property in Form1:
private string myString = "Hello, I'm a sting in Form1"; 
public string MyString 
   { 
   get { return myString; } 
   set {myString = value; }
   }
which does much the same thing, but allows you to change the way the string is handled internally if you need to, and allow you to check values and so forth to ensure your code will work.

Instances are another thing all together. Probably, when you created your instance of Form2, you did it from Form1, or vice versa. If you create Form1 from Form2 and display it as a dialog, then you have the instance ready, and a property is the way to go, just as you do with a OpenFileDialog:
Form1 f = new Form1();
if (f.ShowDialog() == DialogResult.OK)
   {
   Console.WriteLine(f.MyString);
   }
If not, then you have to pass the Form1 instance to Form2 when you create it.

Or better, signal an event in Form1 that Form2 subscribes to, and pass the information in the EventArgs - that way Form2 can decide if it wants the information.
 
Share this answer
 
Comments
esmailian 16-Jun-11 6:00am    
Console.WriteLine(f.MyString);

Does this work in Windwos form app ?
OriginalGriff 16-Jun-11 6:08am    
Yes. If you look in the debugger, you will see the Output tab in the pane that holds your error list - Console output appears here by default. You can move it, but it's generally not worth the bother. Handle for tracing execution while testing though.
RaviRanjanKr 16-Jun-11 7:09am    
Nice Answer, 5+
This is a very popular question of form collaboration, I don't know why.

The most robust way is implementing appropriate interface in the form class.
Please see detail in my past answer:
How to copy all the items between listboxes in two forms[^].

Please see also other suggestions and the discussion.

One more thing: more then one non-modal form is not so good. Try to design all the application in the controls of one main form. You can show, hide or dock away parts of the form (panels, tab pages, whatever). Just a recommendation.

Good luck,
—SA
 
Share this answer
 
Comments
esmailian 16-Jun-11 5:39am    
my5
Sergey Alexandrovich Kryukov 16-Jun-11 11:32am    
Thank you.
--SA
Sweety Khan 16-Jun-11 12:39pm    
nice recommendation. it makes things easy
Sergey Alexandrovich Kryukov 16-Jun-11 13:20pm    
Thank you.
Good luck,
--SA
As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—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