Accessing Form's Control from another class C#

前端 未结 4 1252
礼貌的吻别
礼貌的吻别 2020-12-19 10:38

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

4条回答
  •  青春惊慌失措
    2020-12-19 11:28

    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. Previously you were creating a new form instance rather than updating the current form.

提交回复
热议问题