Q

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

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?
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.

This was first published in January 2003

Dig deeper on ASP.NET development best practices

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close