1

我想在其他班级做某事时更新文本框。让我把我的代码:

Form1.cs

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();
        }

        public void UpdateBox()
        {
            textBox1.Text = "hello";
        }
    }
}

调用类.cs

namespace TestApp
{
    class CalledClass
    {
     public void Call_UpdateBox()
        {
            Form1 mainform = new Form1();

            //do looping for doing some tasks here and update textbox every time
            mainform.UpdateBox();
        }
    }
}

当单击主窗体上的按钮时,会调用 CalledClass 中的 Call_UpdateBox 函数,我必须在其中进行一些循环并在更新主窗体中的文本框之间进行。虽然如果我在调试模式下看到它的值,文本框会更新,但它在主窗体上仍然是空白的。请建议。提前谢谢。

4

4 回答 4

5

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

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();
        }
    }
}
于 2013-06-13T16:35:53.283 回答
2

您正在创建一个新的表单实例,甚至没有显示它。所以你没有调用UpdateBox()好的对象实例。

相反,重新使用您当前的mainForm. 例如:

public void Call_UpdateBox(Form1 targetForm)
{
    targetForm.UpdateBox();
}

private void button1_Click(object sender, EventArgs e)
{
    call.Call_UpdateBox(this);
}
于 2013-06-13T16:36:03.573 回答
2

最简单的方法

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

        public Form1()
        {
            InitializeComponent();
            call.FormH = this;
        }

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

        public void UpdateBox()
        {
            textBox1.Text = "hello";
        }
    }
}

namespace TestApp
{
    class CalledClass
    {
        public static Form1 FormH;

        public void Call_UpdateBox()
        {
            //do looping for doing some tasks here and update textbox every time
            FormH.UpdateBox();
        }
    }
}
于 2013-06-13T16:40:22.520 回答
1

您可以使用Threads更简单的方法,因为您在 WindowsForms 中是使用BackgroundWorker

你的 Form1 会是这样的:

public partial class Form1 : Form
{
    BackgroundWorker _bw = new BackgroundWorker();
    CalledClass call = new CalledClass();

    public Form1()
    {
        InitializeComponent();

        bw.DoWork += bw_DoWork;
        bw_ProgressChanged += bw_ProgressChanged;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (bw.IsBusy != true)
        {
            bw.RunWorkerAsync();
        }
    }

    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        call.Call_UpdateBox();
    }

    private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        textBox1.Text = "hello";

        // Here you can access some progress property from CalledClass in order to monitor and inform progress
    }
}
于 2013-06-13T16:37:20.160 回答