Nyi*_*ght 5 c# visual-studio-2008
我试图更改类中标签的文本,但没有成功。
我所拥有的是我使用 get{} 和 set{} 更新类中的变量。我知道我需要在 set{} 中放置或执行某些操作,以使其将更新值从类发送回 form1 类,以便它可以更新标签。
谁能告诉我如何才能做到这一点?或者如果有一种方法可以从类内更新标签,那就更好了。
我希望这是有道理的,并提前致谢。
您可能需要考虑使用委托或事件,并让您的类将事件返回到表单,这样您的类将不知道您的表单。
例子
class SomeClass
{
public delegate void UpdateLabel(string value);
public event UpdateLabel OnLabelUpdate;
public void Process()
{
if (OnLabelUpdate != null)
{
OnLabelUpdate("hello");
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void UpdateLabelButton_Click(object sender, EventArgs e)
{
SomeClass updater = new SomeClass();
updater.OnLabelUpdate += new SomeClass.UpdateLabel(updater_OnLabelUpdate);
updater.Process();
}
void updater_OnLabelUpdate(string value)
{
this.LabelToUpdateLabel.Text = value;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28829 次 |
| 最近记录: |