[英]How to append text to the richTextBox from another class in C# Winform?
[英]invoking lable text from another class in c# winform
我目前正在public partial class Form1 : Form
使用此代码更新public partial class Form1 : Form
this.Invoke((MethodInvoker)delegate
{
Intellisale_Lastprocessed_Item_Label.Text = "example"
}
因为我有大量的UI元素,所以我宁愿将所有ui更新代码放入另一个类中。
因此,我建立了“ UiUpdate”类。
不幸的是,从此类中我收到消息:“表格1不包含Intellisale_Lastprocessed_Item_Label的定义”
可能我忽略了一些确实很简单的事情,但尚未找到答案。
编辑:
由于这些建议,我将代码更改为以下内容:
class UiUpdate
{
public void UpdateIntellisale(Form1 form)
{
form.Intellisale_Lastprocessed_Item_Label.text = "example";
}
}
不幸的是,我仍然收到消息,没有标签的定义
更新2:
如前所述,标签在设计师内部定义为私有
您正在static
引用Form1
类 ,而不是对该类的特定实例 。 将所需的实例传递给方法:
public void UpdateIntellisale(Form1 form)
{
form.Intellisale_Lastprocessed_Item_Label.Text = "test";
// etc...
}
当您从表单中调用它时,会将其引用传递给表单。 如果从表单本身调用,可能是this
。 基本上,您需要您的帮助程序类/方法来知道它使用的是哪种形式。
您需要将Form1
类的对象传递给UpdateIntellisale
方法。 喜欢:
class UiUpdate {
public void UpdateIntellisale(Form1 form) {
form.Intellisale_Lastprocessed_Item_Label.text = "test";
}
}
并像这样调用: UpdateIntellisale(this)
(当然,仅当this
属于Form1
类时)
存在几个问题:1.您将标签视为已定义为Form1的静态成员。 请改用实例名称。 例如:
Form1 form = new Form1();
form.Intellisale_Lastprocessed_Item_Label.Text = "example"
// Be aware of an InvalidOperationException with the message, "Control control name accessed from a thread other than the thread it was created on." that is throw if you use this code.
请参阅: https : //docs.microsoft.com/zh-cn/dotnet/framework/winforms/controls/how-to-make-thread-safe-calls-to-windows-forms-controls
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.