简体   繁体   English

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

[英]Accessing Form's Control from another class C#

I'm a newbie in c# and visual studio, but not programming in general. 我是c#和visual studio的新手,但不是一般的编程。 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. 我在3天内搜索了我的问题的答案,我发现了很多,但是出于一些奇怪的原因(我确定我错过了一些非常明显的东西)我无法让它发挥作用。 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). 我有一个表单(Form3),带有一个文本框和一个按钮(我设置它只是为了测试目的)。 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. 我知道最合适的方法是使用GET和SET访问器在Form3.cs中创建一个属性。 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: 这是我的示例代码:

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? 有人可以帮我解决这个问题吗?

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

You're creating a brand new Form3() instance. 您正在创建一个new Form3()实例。
This does not affect the existing form. 这不会影响现有表格。

You need to pass the form as a parameter to the method. 您需要将表单作为参数传递给方法。

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 setCodes property. 这会将表单的当前实例传递给另一个类,以便它可以更新setCodes属性。 Previously you were creating a new form instance rather than updating the current form. 以前,您正在创建新的表单实例,而不是更新当前表单。

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 Private to Public in the Form(Designer.cs) 通过在窗体中修改控件PrivatePublic (Designer.cs),轻松访问另一个类中的控件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM