2008-10-20 37 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 
    } 
} 

重要:但是你一定不能忘记好处你有公众宣布修改窗体属性和你可以访问其他明智的,你不能看到从类

相关问题