Access to form control from another C # class

I am new to C # and visual studio, but not programming at all. I searched for the answer to my question for 3 days, and I found a lot of them, but for some strange reason (I'm sure I miss something very obvious). I can not make it work. I think this is the easiest question as I ask. I have a form (Form3) with a text box and a button (I configured it for testing purposes only). I want to fill out and read this text box from another class. I understand that the most appropriate way to do this is to create a property in Form3.cs using GET and SET accessors. I did it, but I can't get it to work. I do not receive any error messages, but I also cannot set the value of the text field. It just stays empty. Here is 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 help me solve this problem?

+5
4

, . - :

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

new Form3().
.

.

+2

:

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

, setCodes. , .

+2

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

Private Public (Designer.cs)

-2

All Articles