Click here to Skip to main content
15,745,433 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more: , +
my project has 2 object : form1, form2.

I stay at form1 create 1 object of form2. how do i access properties and controls of form1 while I'm staying at form2 which was initialized
Updated 2-May-11 5:38am
jim lahey 2-May-11 7:47am    
Would you care to elaborate a little? I mean, you haven't even told us what language you're using. They could be paper forms for all we know..
SaoLamEmDau 2-May-11 7:51am    
i use c# with .net 2005
Wannes Geysen 2-May-11 7:55am    
.net 2005??
thatraja 2-May-11 11:37am    
I think he meant VS 2005 (.NET 2.0)
SaoLamEmDau 7-May-11 21:49pm    
sure, you're right

This is generally the path to spaghetti code. Your visual designer makes those controls private for good reason. In general you should be storing that information in a business object, not just in the UI layer, and binding to it (either formally using the .Net data binding mechanism, or informally by setting properties in initialisation), not binding between two forms.

I recommend you read around data binding. Even if you choose not to use it for your project right now, it will come in useful in the future, and you will understand the issues much better.

One exception is a modal dialog, for example an options form or similar. In that case, the properties cannot change while it is active, and you can set them in the constructor and read them after dismissal. For example:

class Form1 {
 void OnSomeMenuItemClicked(MyOptionsClass options){
  OptionsForm optionsForm = new OptionsForm(options);
  if(DialogResult.OK == optionsForm.ShowDialog(this))

MyOptionsClass is just a data class and the OptionsForm reads and writes to it in its constructor (or a Read method) and SaveTo.

Two modeless windows interacting with the same data should not directly set each other's controls. That way lies madness. For that scenario, .Net's data binding mechanism is almost certainly the right answer.

class Form1 {
 void OnSomeOtherMenuItemClicked(){
  Form2 form2 = new Form2(dataSource);

class Form2 {
 Form2(object dataSource){
  this.CoolTextField.DataBindings.Add('Text', dataSource, 'CoolProperty');
  // ... etc

dataSource is either your business object directly, or a binding class that covers it so that the properties you need for data binding are set up correctly.
Share this answer
Go there to take a look how to communicate between two form.
Link1-[Communication Between Two Forms][^]
Share this answer
SaoLamEmDau 7-May-11 21:59pm    
thanks,i'll try it
take a look at delegates and events.
Share this answer
SaoLamEmDau 2-May-11 7:50am    
i don't understand much about delegate. do you have axample?
Wannes Geysen 2-May-11 7:53am    
then learn it. google it, read some articles, search here on cp, ... We can't help you if you don't want to make an effort.
SaoLamEmDau 2-May-11 7:56am    
public delegate void nd(string nd);
public form1()
public void nd(string nd)
//code here right

i said i don't understand much , not didn't know anything and never want to look up
Please see my past solution based on the interface implemented by the form:
How to copy all the items between listboxes in two forms[^].

There are other techniques discussed around that question.

Share this answer
If form2 needs to directly access controls of form1 or modify its properties, make those properties internal and pass an instance of form1 to form2. Since the properties/controls are internal, the form2 can manipulate them using the instance that was passed to it from form1.

Note: The above is considered bad form.

Instead, add events to form2 that form1 can subscribe to once it has been instantiated. This keeps public and private members separate and encourages better encapsulation.

A tutorial on events[^]
Share this answer
in form2:
form1 fm1=new form1();
fm1.controlname. //u can access if that control modifiers property is public
Share this answer
SaoLamEmDau 2-May-11 7:57am    
thanks, i'll try it

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