C# 为什么文本框上的值未在Form1.cs中更新

C# 为什么文本框上的值未在Form1.cs中更新,c#,.net,winforms,C#,.net,Winforms,我正在制作c#window窗体,很久以前就一直在解决一个问题。 情况是: 我有一个GUI Form1.cs[Design],它由一个按钮和文本框(这里是txtmsg)组成 我在visual studio winform项目中创建了一个类Testing.cs,其中包含如下代码: namespace smallTesting { class Testing { public Testing() { MessageBox.Show

我正在制作c#window窗体,很久以前就一直在解决一个问题。 情况是: 我有一个GUI Form1.cs[Design],它由一个按钮和文本框(这里是txtmsg)组成

我在visual studio winform项目中创建了一个类Testing.cs,其中包含如下代码:

namespace smallTesting
{
    class Testing
    {
        public Testing()
        {
            MessageBox.Show("Connection String Did not found");
            Form1 frm = new Form1(); //I do this in order to have access to 
            //renderMessage() so that i will be able to update my output to             
            //textbox(txtMsg) in this function definition by calling it.
            int i = 1;
            for(;;)
            {
                if (i == 50)
                {
                    break;
                }
                frm.renderMessage(i.ToString());               
                i++;
            }
        }
    }
}
而Form1.cs类为:

namespace smallTesting
{
    public partial class Form1 : Form
    {
        public Form1()
        {
          InitializeComponent();

        }
        private void btnStart_Click(object sender, EventArgs e) //It should work on button click.
        {
            btnStart.Enabled = false;
            Testing tst = new Testing();//Instantiate the class

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public void renderMessage(string str)
        {
            this.txtMsg.Text = str;
            MessageBox.Show("str :" + txtMsg.Text); //It should update my Textbox by 1 to 50 . BUT IT DONT DO.Whereas i can see the counting in the message box popuped.
        }
    }
}
我希望类测试中对renderMessage(stringstr)的函数调用必须更新txtMsg,但它没有更新。为什么?(而弹出的messagebox显示每次调用此函数时字符串都会更新)。为什么我的GUI中没有为每次调用更新txtMsg?如何更新它。
注意:请注意,此txtMsg框更新机制必须从testing.cs转到Form1.cs(而不是Form1.cs到testing.cs)

更改测试类以接收要更新文本框的Form1实例

namespace smallTesting
{
    class Testing
    {
        public Testing(Form1 currentInstance)
        {
            MessageBox.Show("Connection String Did not found");
            int i = 1;
            while(i < 50)
            {
                currentInstance.renderMessage(i.ToString());               
                i++;
            }
        }
    }
}

好的,问题。。在
renderMessage
方法中调用
MessageBox.Show
这是find and dandy。。但是,如果您没有调用
frm.show()
方法,您希望它如何显示呢。。这看起来很简单,很容易纠正。你也调试过代码吗。。?同样在这一行
frm.renderMessage(i.ToString())
我希望调用
.showmodel()
方法,因为这是在循环中进行的。我真的会考虑重构你的代码/逻辑,为什么这是用ASP.NET标记的?如果我理解你的代码流,你有一个Frave1,这是你的主要形式和代码的启动。现在,在这段代码的构造函数中初始化一个测试类实例,并在该实例的构造函数中初始化form1的另一个实例。难怪你什么都没看到。消息将发送到另一个实例,而不是当前实例one@Tim我把它拿走了。我错了。@Steve是的,我知道了。但是如何将值i.string()更新为主窗体(Form1.cs)的txt.Msg?
namespace smallTesting
{
    public partial class Form1 : Form
    {
        public Form1()
        {
          InitializeComponent();
        }
        private void btnStart_Click(object sender, EventArgs e) //It should work on button click.
        {
            btnStart.Enabled = false;
            // Pass the reference of the instance of Form1 that you
            // want to update. Do not let the Testing class creates its
            // own instance of form1, instead use THIS ONE.
            Testing tst = new Testing(this);
        }
        ......
    }
}