从另一种形式访问一种形式的控件

问题描述:

如何从另一种表单访问一种表单的控件?
如何访问它的方法和私有字段?

Hi, How can I accesss the controls of one form from another form?
How can I access it''s methods and private fields?

您可以执行几项操作.首先,您可以使用构造函数或属性将控件传递给其他表单.另一种方法(首选方法)是将您的表单作为接口传递给另一个表单,该接口具有诸如更新"之类的一些方法.如果要更新窗体,则只需调用它(检查下面将值传递给构造函数的选项).您永远不应直接访问私有字段.始终通过方法和/或属性来封装它们.我已经从我之前回答的问题中粘贴了此答案.它们只是在表单之间传递对象的几种选择.使用方法的一个也可以在这里应用.

要在表单之间传递对象,可以遵循以下方法之一:
一种是在其中一种表单上使用属性.
There are several things you can do. First, you could pass the controls to your other form, using a constructor or property. Another method (the preferred one) is passing your Form to the other Form as an Interface, which has some methods like "Update". When you want the Form to update then simply call this (check the option of passing values to constructors below). You should never have direct access to private fields. Always encapsulate them through methods and/or properties. I have pasted this answer from a question I have answered earlier. They are only a few options to pass objects between forms. The one using a Method could also apply here.

To pass Objects between Forms you can follow one of these approaches:
One is using a Property on one of your Forms.
MyForm frm = new MyForm();
// It is possible to set MyValue at any point in your code, as long as you have a reference to an instance of MyForm.
frm.MyValue = "some value";
frm.Show();


另一种是将其传递给构造函数.


Another is passing it to the constructor.

public partial class MyForm : Form
{
   private IMyInterface _aForm;
   // Use an Interface an Implements it in the Form you want to update.
   // Pass that Form to the constructor.
   public MyForm(IMyInterface aForm)
   {
      InitializeComponent();
      _aForm = Form;
      // Possibly use _aForm here.
   }
   // Do stuff with _aForm here.
}

用法如下:

Usage would look like this:

// Make sure 'this' actually implements the Interface.
MyForm frm = new MyForm(this);
frm.Show();


另一种方法是使用方法...


Another approach could be to use a Method...

public partial class MyForm : Form
{
   private String _myValue;
   public void SetValue(String myValue)
   {
      _myValue = myValue;
     // Possibly do stuff with myValue here.
   }
   // Or use _myValue here.
}

用法:

MyForm frm = new MyForm();
frm.Show();
// Once again, you can use this anywhere in your code, as long as you have a reference to the instance of MyForm.
frm.SetValue("some value");

现在您将数据存储在另一个表单,您所需要做的就是在那里处理,编辑等,并在其原始来源的表单中对其进行更新.

希望对您有帮助:)

So now you got your data in another Form, all you need to do is handle it there, edit it etc. and update it in the Form it originally came from.

Hope it helps :)


您无法从form2访问form1控件,因为它们是form1的私有控件.
如果要基于Form2的某些操作设置Form1控件值,则应在Form2中创建事件,并且Form1可以订阅该事件.当Form2发生任何更改时,将触发该事件并将信息作为form1可以访问的eventargs传递.

请注意:您永远不能直接从Form2访问form1控件或私有字段,因为它们是私有的.
You can''t access form1 controls from form2, since they are private to the form1.
If you want to set Form1 control value based on Form2''s some action then you should create Event in Form2 and Form1 can subscribe to the event. When any change happen on Form2 fire the event and pass the information as eventargs which form1 can access.

Please note: you can never access form1 control or private fields directly from Form2 since they are private.


这是有关表单协作的常见问题.最健壮的解决方案是在Form类中实现适当的接口,并传递接口引用而不是对Form的整个实例"的引用.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

I must add that the best UI design uses just one form; and everything which needs to be changed or navigated happens inside that form: docking, tabbing interfaces, etc.

—SA