通过从类调用到主窗体来更新控件和刷新窗体 [英] Update control and refresh form by call from class to main form
本文介绍了通过从类调用到主窗体来更新控件和刷新窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在做其他课程时更新文本框.让我把我的代码:
I want to update text box while doing something in other class. Let me put my code :
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";
}
}
}
CalledClass.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 函数在主窗体上的按钮被单击时被调用,我必须在其中执行一些循环并在更新主窗体中的文本框之间进行.虽然如果我在调试模式下看到它的值,文本框会得到更新,但它在主窗体上仍然是空白的.请建议.提前谢谢.
The Call_UpdateBox function in CalledClass is called when the button on main form is clicked, where I have to do some looping and in between update the textbox in the main form. Though textbox gets updated if i see its value in debug mode, but it reamins blank on main form. Please suggest. Thx in advance.
推荐答案
您正在声明 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();
}
}
}
这篇关于通过从类调用到主窗体来更新控件和刷新窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文