如何访问在C#中的usercontrol属性 [英] How to access properties of a usercontrol in C#

查看:751
本文介绍了如何访问在C#中的usercontrol属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个C#用户控件有一个TextBox和一个RichTextBox的。

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

如何访问RichTextBox中的属性从用户控件之外。

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

例如..如果我把它放在一个形式,我怎么能使用的RichTextBox的文本propertie ???

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

感谢

推荐答案

干净的方法是为你的用户控件的属性暴露所需的性能,例如:

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.

这篇关于如何访问在C#中的usercontrol属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆