如何在 C# 中访问用户控件的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/411316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:20:09  来源:igfitidea点击:

How to access properties of a usercontrol in C#

c#user-controlspropertiesrichtextbox

提问by Pedro Luz

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

我制作了一个带有一个文本框和一个富文本框的 C# 用户控件。

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

例如..如果我把它放在一个表单中,我如何使用richtextbox的Text属性???

thanks

谢谢

采纳答案by M4N

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.

这种方法的另一个优点是它隐藏了用户控件的内部实现。如果您想将富文本控件与不同的控件交换,则不会破坏控件的调用者/用户。

回答by Robert Venables

Change the access modifier ("Modifiers") of the RichTextBox in the property grid to Public.

将属性网格中 RichTextBox 的访问修饰符(“Modifiers”)更改为 Public。

回答by Fabrizio C.

Add a property to the usercontrol like this

像这样向用户控件添加一个属性

public string TextBoxText
{
    get
    {
        return textBox1.Text;
    }
    set
    {
        textBox1.Text = value;
    }
}

回答by Timothy Carter

You need to make a public property for the richtextbox, or expose some other property that does the job of setting the richtextbox text like:

您需要为富文本框创建一个公共属性,或者公开一些其他属性来设置富文本框文本,例如:

private RichTextBox rtb;

public string RichTextBoxText
{
    get
    {
        return rtb.Text;
    }
    set
    {
        rtb.Text = value;
    }
}

回答by JYelton

I recently had some issues doing this with a custom class:

我最近在使用自定义类时遇到了一些问题:

A user control had a public property which was of a custom class type. The designer by default tries to assign some value to it, so in the designer code, the line userControlThing.CustomClassProperty = nullwas being automatically added.

用户控件具有自定义类类型的公共属性。默认情况下,设计器会尝试为其分配一些值,因此在设计器代码中,userControlThing.CustomClassProperty = null会自动添加该行。

The intent was to be able to provide the user control with a custom class at any point while running the program (to change values visible to the user). Because the set {}portion did not check for null values, various errors were cropping up.

目的是能够在运行程序的任何时候为用户控件提供自定义类(以更改用户可见的值)。由于该set {}部分没有检查空值,因此会出现各种错误。

The solution was to change the property to a private one, and use two public methods to set and get the value. The designer will try to auto-assign properties, but leaves methods alone.

解决方案是将属性更改为私有属性,并使用两个公共方法来设置和获取值。设计器将尝试自动分配属性,但不理会方法。