从另一个类 C# 访问表单的控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10576856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:21:48  来源:igfitidea点击:

Accessing Form's Control from another class C#

c#

提问by Tony

I'm a newbie in c# and visual studio, but not programming in general. I searched for answer to my question for 3 days and I found plenty of them, but for some weird reason (I'm sure I'm missing something very obvious) I cannot get it to work. I think it's the most basic question newbies like me ask. I have a form (Form3) with a text box and a button (I set it up is just for testing purposes). I want to populate and read this text box from another class. I understand the most proper way to do this is to create a property in Form3.cs with GET and SET accessors. I did that but I cannot get it to work. I'm not getting any error messages, but I'm not able to set the value of the text box either. It just remains blank. Here's my sample code:

我是 c# 和 Visual Studio 的新手,但一般不会编程。我搜索了我的问题的答案 3 天,我找到了很多答案,但由于一些奇怪的原因(我确定我遗漏了一些非常明显的东西)我无法让它工作。我认为这是像我这样的新手问的最基本的问题。我有一个带有文本框和按钮的表单(Form3)(我设置它只是为了测试目的)。我想从另一个班级填充和阅读此文本框。我明白最正确的方法是在 Form3.cs 中使用 GET 和 SET 访问器创建一个属性。我这样做了,但我无法让它工作。我没有收到任何错误消息,但我也无法设置文本框的值。它只是保持空白。这是我的示例代码:

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public string setCodes
        {
            get { return test1.Text; }
            set { test1.Text = value; }
        }

        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {   }

        private void button1_Click(object sender, EventArgs e)
        {
            a.b();
        }
    }

    public class a
    {       
        public static void b()
        {
            Form3 v = new Form3();
            v.setCodes = "abc123";
        }
    }
}

Can someone lend me a hand solving this?

有人可以帮我解决这个问题吗?

回答by SLaks

You're creating a brand new Form3()instance.
This does not affect the existing form.

您正在创建品牌new Form3()实例。
这不会影响现有表格。

You need to pass the form as a parameter to the method.

您需要将表单作为参数传递给方法。

回答by Ivo

The problem is you are setting the value to a new instance of the form. Try something like this:

问题是您正在将值设置为表单的新实例。尝试这样的事情:

public partial class Form3 : Form {
    public string setCodes
    {
        get { return test1.Text; }
        set { test1.Text = value; }
    }

    private A a;

    public Form3()
    {
        InitializeComponent();
        a = new A(this);
    } 

    private void button1_Click(object sender, EventArgs e)
    {            
        a.b();            
    }


    private void Form3_Load(object sender, EventArgs e)
    {

    }
}

public class A
{       
    private Form3 v;

    public a(Form3 v)
    {
        this.v = v;
    }

    public void b()
    {
        v.setCodes = "abc123";
    }
}    

回答by Enigmativity

Try this:

尝试这个:

public partial class Form3 : Form
{
    /* Code from question unchanged until `button1_Click` */

    private void button1_Click(object sender, EventArgs e)
    {
        a.b(this);
    }
}

public class a
{       
    public static void b(Form3 form3)
    {
        form3.setCodes = "abc123";
    }
}

This passes the current instance of the form to the other class so that it can update the setCodesproperty. Previously you were creating a new form instance rather than updating the current form.

这会将表单的当前实例传递给另一个类,以便它可以更新setCodes属性。以前您是在创建一个新的表单实例,而不是更新当前的表单。

回答by Sri_kanth0526

Sending form instance to other other class

将表单实例发送到其他类

Form1 objForm1=new Form1();
obj.Validate(objForm1);

Easy way to access controls in another class by modifying Controls Privateto Publicin the Form(Designer.cs)

简单的方法在另一类访问控制通过修改控制Private,以Public在表格(Designer.cs)