[英]How to access a non-static property from another class
I have a non-static property inside a non-static MainForm
class:我在非静态
MainForm
类中有一个非静态属性:
public string SelectedProfile
{
get { return (string)cbxProfiles.SelectedItem; }
set { cbxProfiles.SelectedItem = value; }
}
I would like to get the value of this property, from another non-static class.我想从另一个非静态类获取此属性的值。 Using
MainForm.SelectedProfile
gives an error saying "An object reference is required for the non-static field, method or property".使用
MainForm.SelectedProfile
会出现错误,提示“非静态字段、方法或属性需要对象引用”。
Usually I would solve this problem by making SelectedProfile
static, but I can't, since cbxProfiles (a ComboBox control) can't be made static.通常我会通过将
SelectedProfile
设为静态来解决这个问题,但我不能,因为 cbxProfiles(一个 ComboBox 控件)不能设为静态。
So how do I access the property's value without making it static?那么如何在不使其静态化的情况下访问该属性的值呢?
You access non-static members the same way you always do: by using a reference to an instance of the object.您可以像往常一样访问非静态成员:通过使用对对象实例的引用。
So whatever code you want to be able to use that property, you need to pass it a reference to a MainForm
object.因此,无论您希望能够使用该属性的代码是什么,都需要向它传递一个对
MainForm
对象的引用。
As said in the compilation error, you need to have a reference of the existing MainForm
instance to act on it.正如编译错误中所说,您需要引用现有的
MainForm
实例才能对其进行操作。
// You surely do this somewhere in your code
MainForm mainForm = new MainForm();
// ...
// Use the reference to your mainForm to access its public properties
String selectedProfile = mainForm.SelectedProfile;
I might be late to the party but my solution might help someone someday down the road.我可能会迟到,但我的解决方案可能会在未来的某一天帮助某人。 You can directly access an open form's controls (even private ones) using
Application.OpenForms[n]
...您可以使用
Application.OpenForms[n]
直接访问一个开放表单的控件(甚至是私有的)...
For example, let's assume you have created a MainForm and then created a comboBox such that it is inside MainForm => Tab (named tabControl) => TabPage (named tabPageMain) => Panel (named pnlMain) => ComboBox (named cmbSeconds).例如,假设您已经创建了一个 MainForm,然后创建了一个组合框,使其位于 MainForm => Tab(名为 tabControl)=> TabPage(名为 tabPageMain)=> Panel(名为 pnlMain)=> ComboBox(名为 cmbSeconds)。 Then you can access this last control as follows:
然后您可以按如下方式访问最后一个控件:
ComboBox combo = Application.OpenForms[0].Controls["tabControl"].Controls["tabPageMain"].Controls["pnlMain"].Controls["cmbSeconds"] as ComboBox;
string SelectedProfile = (string)combo.SelectedItem;
// OR
bool isMaximized = Application.OpenForms[0].WindowState == FormWindowState.Maximized;
ie you've got to traverse the path from the top-level form down to that particular control.即,您必须遍历从顶级表单向下到该特定控件的路径。 Document Outline view of Visual Studio ( View menu => Other Windows => Document Outline ) might help you in this bcoz you might be overlooking some transparent containers in between.
Visual Studio 的文档大纲视图(查看菜单 =>其他 Windows =>文档大纲)可能会帮助您解决此问题,因为您可能会忽略中间的一些透明容器。
Use it cautiously.请谨慎使用。 For example, if the handles of any of the referenced controls are not yet created, you might see runtime exceptions.
例如,如果尚未创建任何引用控件的句柄,您可能会看到运行时异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.