简体   繁体   English

.NET - 在表单上获取控件引用的正确方法

[英].NET - Proper way to get reference of a control on the form

I have a C# class library which uses a form (which is also in the library). 我有一个C#类库,它使用一个表单(也在库中)。 Let's say I have an edit box on this form called editContents . 假设我在这个表单上有一个名为editContents的编辑框。 In a regular form application, I'm used to being able to aquire the edit box like this: 在常规表单应用程序中,我习惯于能够像这样获取编辑框:

class MainForm
{
     void Method()
     {
          this.editContents.Text = "Hi";
     }
}

I guess some magic occurs behind the scenes in a regular forms application, because the edit box member is private in the MainForm class, but I can still access it like a public member. 我想在常规表单应用程序中幕后发生了一些魔术,因为编辑框成员在MainForm类中是私有的,但我仍然可以像公共成员一样访问它。

But in my class library I can't access the edit box like this. 但在我的类库中,我无法像这样访问编辑框。 I instantiate and show the form "manually" like this: 我实例化并显示“手动”形式,如下所示:

form = new MyForm();
form.Show();

How do I properly acquire the editContents control from this form? 如何从此表单正确获取editContents控件?

You could make it a publicly accessible Property , by adding some code like this: 您可以通过添加以下代码使其成为可公开访问的属性

public String EditContents // for just the "Text" field
{
   get { return this.editContents.Text; }
   set { this.editContents.Text = value; }
}

Or: 要么:

public TextBox EditContents // if you want to access all of the TextBox
{
   get { return this.editContents; }
   set { this.editContents = value; }
}

The "magic" is that a field is generated for your textbox in the *.Designer.cs file. “神奇”是在* .Designer.cs文件中为您的文本框生成一个字段。 By default, this field is private. 默认情况下,此字段是私有的。 If you want to change it's accessibility, select your text box in the forms designer, and change the "Modifiers" property to Public. 如果要更改其可访问性,请在表单设计器中选择文本框,然后将“修饰符”属性更改为“公共”。

This might however not be a good idea to expose publicly all controls of your form. 但是,公开展示表单的所有控件可能不是一个好主意。 You can instead wrap it around in a property like Donut suggests, which is cleaner. 您可以将其包裹在Donut建议的属性中,这样更清洁。

Private members are accessible within their declaring class. 私人会员可以在宣布课程中访问。 That's why you're able to access editContents from within MainForm. 这就是为什么你能够从MainForm中访问editContents的原因。
Private members are inaccessible from outside their declaring class. 私人会员在宣布课程之外无法进入。
(it's the definition of private , there is no magic) (这是private的定义,没有魔法)

You can wrap it in a public property: 您可以将其包装在公共财产中:

public TextBox EditContents
{
   get { return this.editContents; }
}

In the first instance, you are able to access editContents because you are within the scope of the form. 在第一个实例中,您可以访问editContents因为您在表单的范围内。

As @Donut has said, you can expose a property for users of your library to play around with the control. 正如@Donut所说,您可以为您的库的用户公开一个属性来使用该控件。 If you want to limit the access, you could write a method instead. 如果要限制访问权限,可以编写方法。

eg 例如

void SetContentForEditor(string text)
{
   editContent.Text = text;
}

And, you can then make a call to SetContentForEditor 然后,您可以调用SetContentForEditor
eg 例如

myForm.SetContentForEditor("hello world");

您可以通过Controls集合获取它。

form.Controls.Find("nameOfControl", true);

访问Method()函数中的表单成员与通过上一个代码段中的form引用的区别在于您的Method()函数是表单类的成员,因此可以访问该类的其他私有成员。

I would introduce an event in the library which notifies all subscribers to this event when a change happens to the text and should be set in the form. 我将在库中引入一个事件,当文本发生更改并应在表单中设置时,该事件会通知所有订阅者此事件。 The form should attach to this event and set the textbox content on its own. 表单应附加到此事件并自行设置文本框内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Lucene .NET中获得读者/作家的正确方法 - Proper way to get readers/writers in Lucene .NET 使用.NET 4实体框架删除引用的正确方法是什么? - What is the proper way to remove a reference using the .NET 4 Entity Framework? ASP.NET-初始化在转发器中创建的用户控件的正确方法? - ASP.NET - Proper way to initialize a user control created in a repeater? 以编程方式触发ASP.NET控件事件的正确方法是什么? - What is the proper way to fire an ASP.NET control event programatically? 有没有办法在.NET中获取Flash控件中的所有变量? - Is there a way to get all the variables in flash control in .NET? ASP.NET:获取对父ASCX控件的强类型引用 - ASP.NET: Get a strongly typed reference to a parent ASCX control 是否有任何其他适当的方法可以对 C# 表单中的两个不同选项卡进行相同的控制? - Is there any other proper way to have same control on two different tabs in C# form? 这是获取 WebProfile 的正确方法吗? - Is this a proper way to get a WebProfile? 在ASP.NET(C#)中,呈现和引用基于集合的表单的正确方法是什么? - In ASP.NET (C#), what is the proper way to render and reference collection-based forms? 处理新表格的正确方法 - Proper way to dispose a new Form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM