2014-10-27 25 views
1

我對C#非常陌生,我無法想象從另一個文件到達變量,表單屬性和駐留在一個文件中的其他方法。我覺得,這應該像#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.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'

回答

1

在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. 
     } 
    } 
} 
+0

在其他類中的方法設置名稱,返回的字符串類型,並在它自己的類設置的名稱,而不是似乎是一個更好的選擇。感謝所有回覆。 – ssd 2014-10-27 10:37:26

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

當你說

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

如果你真的想在Form1.TextClass1設置,那麼你應該將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();  
} 
相關問題