Home > Ask the Microsoft .Net Development Experts > Visual Studio .NET Questions & Answers > In C#, how can I change the properties of controls on another form?
Ask The Win Development Expert: Questions & Answers
EMAIL THIS

In C#, how can I change the properties of controls on another form?

Chris Sells EXPERT RESPONSE FROM: Chris Sells

Pose a Question
Other Win Development Categories
Meet all Win Development Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 20 January 2003
In C#, I need to change the properties of controls on another form. For example, with a label control on Form 1, how can I change its text property from Form 2?


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Ask the Expert
Read data from parallel port
Escape from mailto:
How do I add images in listview by using VB.NET?
What is the best technique for connecting to a database from VB.NET?
How do you use control collection in VB.NET?
Is ADO.NET record locking no longer an option?
How do I print a form in VB.NET?
How do I get my start page back?
Using an array, how can I create a combo box that lists a dropdown menu for units of length?
How do I determine the type of a control on a Windows Form in VB.NET?

Visual Studio .NET (Archive)
What could be causing this problem with my add New and delete methods?
Is there a .NET equivalent to netsend that I can use in VB.NET?
I'm getting a lot of flickering on my application
How can I make a VB6 network app available on the Internet using .NET?
How can I implement an FTP upload using VB.NET?
VB.NET app works on my local drive but not on the network drive
Is developing add-ons for Windows Explorer supported using managed code in .NET?
How do I add images in listview by using VB.NET?
What is the best technique for connecting to a database from VB.NET?
How do you use control collection in VB.NET?

Visual Studio .NET
Load a DSL domain model instance file programmatically
Calling via command prompt from ASP.NET
On line-by-line analysis in VS .NET
Best approach for merging text files using arrays
Descriptions of symbols in the class view of a .NET solution
What's the best approach for enumerating network resources?
How to enable a line-by-line code walkthrough
Changing a C++ Makefile project to a Utility project
How can I configure Visual Studio.NET 2002 to use the .NET Framework 1.1?
Changing a datagrid column to read-only=false based on variable

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


You have two options for exposing properties of hosted controls from a form. The intuitive option is simply to make the control field public:


class Form1 : Form {
  public Label labelInForm1; // Don't!
  ...
}

Now, when Form 2 needs access to the control in Form 1, it can access it directly, like so:


class Form2 : Form {
  Form1 form1; // Needs to be set from somewhere

  void setLabelInForm1_Click(object sender, EventArgs e) {
    // Set a control's property on Form1
    form1.labelInForm1.Text = textForLabelInForm1.Text;
  }
  ...
}

For this to work, the Form 2 object needs to have access to the Form 1 object. Form 2 would normally get access to an object by exposing a property so that whoever has access to Form 1 can share that access with Form 2:


class Form2 : Form {
  ...
  Form1 form1; // Set by the property

  public Form1 Form1 {
    get { return form1; }
    set { form1 = value; }
  }
}

For example, maybe Form 1 creates Form 2 and sets the Form 1 property as it does so:

void Form1_Load(object sender, EventArgs e) {
  Form2 form2 = new Form2();
  form2.Form1 = this; // Allow Form2 to access Form1 public members
  form2.Show();
}

Now, Form 2 has access to all public members of Form 1, including any controls exposed as public fields. However, there's a problem. The problem is that if Form 1 decides to make the Label control editable, it may well need to change the Label to a TextBox. Now, because the field has been exposed to the world, whenever Form1's implementation needs to change, it has to update the client code as well, or the compiler will complain. Instead of exposing fields directly, it's almost always better to expose public properties whose implementation can change, but whose signature remains the same:

class Form1 : Form {
  ...
  private Label labelInForm1; // Fields should be private
  public string LabelText {
    get { return labelInForm1.Text; }
    set { labelInForm1.Text = value; }
  }
}

Once you use a property, the access code is actually simpler, since Form 2 is accessing Form 1 and not worrying about the internal implementation details:

void setLabelInForm1_Click(object sender, EventArgs e) {
  // Set a property on Form1 (w/o worrying about the implementation)
  form1.LabelText = textForLabelInForm1.Text;
}

Now, when Form1 needs to change its implementation from a Label control to a TextBox control, it can do so:

class Form1 : Form {
  ...
  private TextBox textboxInForm1; // Not exposed to client
  public string LabelText { // Client need not change
    get { return textboxInForm1.Text; }
    set { textboxInForm1.Text = value; }
  }
}

By keeping fields private, internal implementation details can change w/o affecting clients. This is encapsulation and abstraction, both core principles of object-orientation.




Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Windows Development - White Papers, News and Expert Advice
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts