簡體   English   中英

為什么我無法從其他文件訪問表單屬性?

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

我是C#新手,我無法從另一個文件中找出存在於一個文件中的變量,表單屬性和其他一些方法。 我覺得,這應該像#include指令一樣簡單,但找不到方法。

這是我在VS中創建一個新的Windows窗體應用程序時由auto創建的文件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.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!
        }
    }
}

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

根據您的代碼, 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();

當你說

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

在C#Windows窗體應用程序中,每個窗體都是一個類,如果要訪問窗體的屬性,則必須創建如下對象:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM