Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access properties of a usercontrol in C#

Tags:

I've made a C# usercontrol with one textbox and one richtextbox.

How can I access the properties of the richtextbox from outside the usercontrol.

For example.. if i put it in a form, how can i use the Text propertie of the richtextbox???

thanks

like image 974
Pedro Luz Avatar asked Jan 04 '09 17:01

Pedro Luz


People also ask

What is a UserControl?

A UserControl is a collection of controls placed together to be used in a certain way. For example you can place a GroupBox that contains Textbox's, Checkboxes, etc. This is useful when you have to place the same group of controls on/in multiple forms or tabs.


1 Answers

Cleanest way is to expose the desired properties as properties of your usercontrol, e.g:

class MyUserControl
{
  // expose the Text of the richtext control (read-only)
  public string TextOfRichTextBox
  {
    get { return richTextBox.Text; }
  }
  // expose the Checked Property of a checkbox (read/write)
  public bool CheckBoxProperty
  {
    get { return checkBox.Checked; }
    set { checkBox.Checked = value; }
  }


  //...
}

In this way you can control which properties you want to expose and whether they should be read/write or read-only. (of course you should use better names for the properties, depending on their meaning).

Another advantage of this approach is that it hides the internal implementation of your user control. Should you ever want to exchange your richtext control with a different one, you won't break the callers/users of your control.

like image 83
M4N Avatar answered Oct 14 '22 16:10

M4N