2008-10-20 69 views
24

我正在研究C#程序,現在我有一個Form和幾個類。我希望能夠訪問我的課程中的一些Form控件(例如TextBox)。當我嘗試在TextBox文本從我的課改變我得到以下錯誤:如何從C#中的類訪問表單方法和控件?

An object reference is required for the non-static field, method, or property 'Project.Form1.txtLog'

如何訪問方法和在Form1.cs從我類之一控件?

回答

28

您試圖訪問類,而不是對象。這種說法可能會讓初學者感到困惑,但是你正試圖通過撬開房屋計劃的大門來打開房門。

如果你真的想直接從一個類訪問表單組件(你沒有),你可以使用實例化你的表單的變量。

取決於你想要去的你會更好哪種方式或者發送控制或任何的文本的方法在你的類,例如在你的窗體類和設置

public void DoSomethingWithText(string formText) 
{ 
    // do something text in here 
} 

或暴露性質在那裏格式的文本 - 例如

string SomeProperty 
{ 
    get 
    { 
     return textBox1.Text; 
    } 
    set 
    { 
     textBox1.Text = value; 
    } 
} 
+0

形式的控制在「textBox1.Text =價值」其中值是從哪裏來的? – user128807 2010-12-29 16:13:06

+0

如何使用此方法刷新網格數據? – 2011-04-12 14:44:10

9
  1. ,你必須有以表單對象的引用來訪問它的元素
  2. 元素必須聲明爲public爲了其他類訪問他們
  3. 沒有這樣做 - 你班級必須知道你的表格是如何實施的;請勿在表單類別
  4. 以外的表單控件中公開屬性以獲取/設置您感興趣的值
  5. 發佈您想要的更多詳細信息以及爲什麼,這聽起來像您可能是在不具有良好的封裝做法
3

您需要訪問對象一致的方向前進關閉....你不能簡單地問窗體類....

如...

你會做一些事情,如

Form1.txtLog.Text = "blah" 

代替

Form1 blah = new Form1(); 
blah.txtLog.Text = "hello" 
1

你需要讓會員在Form類是公共的,或者,如果服務類是在同一程序內部。 Windows控件的可見性可以通過它們的Modifiers屬性進行控制。

請注意,將服務類明確綁定到UI類通常被認爲是不好的做法。而應該在服務類和表單類之間創建良好的接口。這就是說,爲了學習或者只是一般的混亂,如果你暴露了表單成員的服務類別,地球就不會脫離它的軸心。

RP

14

另一解決方案是通過文本框(或控制要修改)到將操縱它作爲一個參數的方法。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     TestClass test = new TestClass(); 
     test.ModifyText(textBox1); 
    } 
} 

public class TestClass 
{ 
    public void ModifyText(TextBox textBox) 
    { 
     textBox.Text = "New text"; 
    } 
} 
2

如果表單啓動第一,在窗體加載處理程序,我們可以實例我們班的一個副本。我們可以擁有引用我們想要引用的控件的屬性。將引用形式'this'傳遞給該類的構造函數。

public partial class Form1 : Form 
{ 
    public ListView Lv 
    { 
     get { return lvProcesses; } 
    } 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     Utilities ut = new Utilities(this); 
    } 
} 

在你的類中,表單的引用被傳遞給構造函數並作爲私有成員存儲。此表單引用可用於訪問表單的屬性。

class Utilities 
{ 
    private Form1 _mainForm; 
    public Utilities(Form1 mainForm) 
    { 
     _mainForm = mainForm; 
     _mainForm.Lv.Items.Clear(); 
    } 
} 
1

我是比較新的c#和全新的stackoverflow。無論如何,關於如何從類的表單訪問控件的問題:我剛剛使用了表單的ControlCollection(Controls)類。

 //Add a new form called frmEditData to project. 
     //Draw a textbox on it named txtTest; set the text to 
     //something in design as a test. 
     Form frmED = new frmEditData(); 
     MessageBox.Show(frmED.Controls["txtTest"].Text); 

工作對我來說,也許它會在這兩個問題上的援助。

0

JUST您可以發送表單類像這樣

Class1 excell = new Class1(); //you must declare this in form as you want to control 

excel.get_data_from_excel(this); // And create instance for class and sen this form to another class 

課內當你創建CLASS1

class Class1 
{ 
    public void get_data_from_excel (Form1 form) //you getting the form here and you can control as you want 
    { 
     form.ComboBox1.text = "try it"; //you can chance Form1 UI elements inside the class now 
    } 
} 

重要:但是你一定不能忘記好處你有公衆宣佈修改窗體屬性和你可以訪問其他明智的,你不能看到從類