且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过从类调用到主窗体来更新控件和刷新窗体

更新时间:2023-12-06 12:33:10

您正在声明 Form1 的新实例,而不是引用已经存在的实例.你应该:

You're declaring a new instance of Form1, not referencing the one that already exists. You should:

namespace TestApp
{
    public partial class Form1 : Form
    {
        CalledClass call = new CalledClass();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            call.Call_UpdateBox(this);
        }

        public void UpdateBox()
        {
            textBox1.Text = "hello";
        }
    }
}
namespace TestApp
{
    class CalledClass
    {
     public void Call_UpdateBox(Form1 Sender)
        {
            //do looping for doing some tasks here and update textbox every time
            sender.UpdateBox();
        }
    }
}