简体   繁体   English

为什么我无法从其他文件访问表单属性?

[英]Why I can't reach form properties from another file?

I'm very new to C# and I couldn't figure out reaching variables, form properties and some other methods that reside in one file, from another file. 我是C#新手,我无法从另一个文件中找出存在于一个文件中的变量,表单属性和其他一些方法。 I feel like, this should be as simple as an #include directive, but couldn't find a way. 我觉得,这应该像#include指令一样简单,但找不到方法。

This is the code in file Form1.cs created by auto when I create a new Windows Form app in VS. 这是我在VS中创建一个新的Windows窗体应用程序时由auto创建的文件Form1.cs的代码。 I can reach class definitions that reside in another file named Class1.cs ; 我可以访问位于另一个名为Class1.cs文件中的类定义; no problem here: 这里没问题:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace reach_test {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e) {
            Class1 formName = new Class1(); // this line compiles OK.
            formName.someMethod();          // this line compiles OK.
            this.Text = "Some Header";      // this line compiles OK.
        }
    }
}

And this is the code in Class1.cs file, which I added after. 这是我在之后添加的Class1.cs文件中的代码。 I can not reach, for example, Form1.Text property, which reside in file Form1.cs : 我无法访问,例如, Form1.Text属性,它位于文件Form1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace reach_test {
    public class Class1 {
        public string myText;
        public Class1() {
            myText = "Windows Header";
        }
        public void someMethod() {
            Form1.Text = myText; // this line does not compile!
        }
    }
}

Error message is: An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Control.Text.get' 错误消息是: An object reference is required for the non-static field, method, or property 'System.Windows.Forms.Control.Text.get'

According to your code Form1 is a class name and so static properties only can be accesses in such a manner. 根据您的代码, Form1是一个类名 ,因此静态属性只能以这种方式访问​​。 Text property is not declared as being static , that's why you have to create an instance : Text属性未声明为static ,这就是您必须创建实例的原因

  Form1 myForm = new Form1(); // <- myForm is an instance of Form1 class

  myForm.Text = myText;

  // Probably, you want to do it as well...
  myFrom.Show();

When you say 当你说

    Form1.Text = myText; // this line does not compile!

You are trying to Call ClassName.PropertyName in your Class1 which is wrong, because it should be InstanceName.PropertyName . 您试图在Class1中调用ClassName.PropertyName是错误的,因为它应该是InstanceName.PropertyName More over you don't have access to Form1's instance from your Class1. 您无法访问Class1中的Form1实例。

The best thing you can do is 你能做的最好的事情是

public class Class1 {
    public string myText;
    public Class1() {
        myText = "Windows Header";
    }
}
private void Form1_Load(object sender, EventArgs e) {
    Class1 formName = new Class1(); 
    this.Text = formName.myText;     
}

If you really want to set the Form1.Text within Class1 then you should pass your Form1's instance to Class1 like below. 如果你真的想在Class1设置Form1.Text ,那么你应该将Form1的实例传递给Class1如下所示。

public class Class1 {
    private string _myText;
    private Form _form1;
    public Class1(Form form1) {
        _myText = "Windows Header";
        _form1 = form1;
    }
    public DoSomething(){
        _form1.Text = _myText;
    }
}
private void Form1_Load(object sender, EventArgs e) {
    Class1 formName = new Class1(); 
    formName.DoSomething();     
}

In C# Windows Form application each Forms is a class, if you want to access Form's property you must create an object like this: 在C#Windows窗体应用程序中,每个窗体都是一个类,如果要访问窗体的属性,则必须创建如下对象:

 Form1 myForm = new Form1();
myForm.Show()

then set public property 然后设置公共财产

  myForm.Text = "My text";

But i advice change you'r code to this: 但我建议改变你的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace reach_test {
    public class Class1 {
        public string myText;
        public Class1() {
            myText = "Windows Header";
        }
        public string someMethod() {
           return myText; 
        }
    }
}

then set Form1.Text : 然后设置Form1.Text

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace reach_test {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e) {
            Class1 formName = new Class1(); // this line compiles OK.
           this.text= formName.someMethod();          // this line compiles OK.
        }
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM