【问题标题】:Why I can't reach form properties from another file?为什么我无法从另一个文件访问表单属性?
【发布时间】:2014-12-22 11:29:30
【问题描述】:

我对@9​​87654321@ 很陌生,我无法从另一个文件中找到驻留在一个文件中的变量、表单属性和其他一些方法。我觉得,这应该像 #include 指令一样简单,但找不到方法。

这是我在 VS 中创建新的 Windows 窗体应用程序时自动创建的文件 Form1.cs 中的代码。我可以访问位于另一个名为 Class1.cs 的文件中的类定义;这里没问题:

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.
        }
    }
}

这是Class1.cs 文件中的代码,这是我在之后添加的。例如,我无法访问位于文件Form1.cs 中的Form1.Text 属性:

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!
        }
    }
}

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

【问题讨论】:

    标签: c# class public


    【解决方案1】:

    根据您的代码Form1 是一个类名,因此静态属性 只能以这种方式访问​​。 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();
    

    【讨论】:

      【解决方案2】:

      当你说

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

      您试图在 Class1 中调用 ClassName.PropertyName,这是错误的,因为它应该是 InstanceName.PropertyName。此外,您无法从 Class1 访问 Form1 的实例。

      你能做的最好的事情是

      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;     
      }
      

      如果你真的想在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();     
      }
      

      【讨论】:

        【解决方案3】:

        在 C# Windows Form 应用程序中,每个 Forms 都是一个类,如果你想访问 Form 的属性 你必须像这样创建一个对象:

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

        然后设置公共属性

          myForm.Text = "My text";
        

        但我建议将您的代码更改为:

        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; 
                }
            }
        }
        

        然后设置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.
                }
            }
        }
        

        【讨论】:

        • 与其在另一个类方法中设置名称相反,将名称作为字符串返回并在它自己的类中设置名称似乎是更好的选择。感谢所有回复。
        猜你喜欢
        • 2021-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-07
        相关资源
        最近更新 更多