Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WinC窗体中从另一个类调用标签文本_C#_Winforms - Fatal编程技术网

C# 在WinC窗体中从另一个类调用标签文本

C# 在WinC窗体中从另一个类调用标签文本,c#,winforms,C#,Winforms,我目前正在使用公共部分类Form1:Form中的代码更新我的表单 因为我有大量的UI元素,所以我宁愿把我所有的UI更新代码放到另一个类中 因此,我构建了UiUpdate类。 不幸的是,我从这个类中收到了以下消息: 表格1不包含Intellisale\u Lastprocessed\u Item\u标签的定义 也许我忽略了一些非常简单的事情,但我还没有找到答案 编辑: 由于这些建议,我将代码更改为: class UiUpdate { public void UpdateIntellisal

我目前正在使用公共部分类Form1:Form中的代码更新我的表单

因为我有大量的UI元素,所以我宁愿把我所有的UI更新代码放到另一个类中

因此,我构建了UiUpdate类。 不幸的是,我从这个类中收到了以下消息: 表格1不包含Intellisale\u Lastprocessed\u Item\u标签的定义

也许我忽略了一些非常简单的事情,但我还没有找到答案

编辑:

由于这些建议,我将代码更改为:

class UiUpdate
{
    public void UpdateIntellisale(Form1 form)
    {
        form.Intellisale_Lastprocessed_Item_Label.text = "example";
    }
}
不幸的是,我仍然收到这样一条消息:没有标签的定义

更新2:

如前所述,标签在设计器中定义为私有的

您正在对Form1类进行静态引用,而不是该类的特定实例。将所需的实例传递给方法:

public void UpdateIntellisale(Form1 form)
{
    form.Intellisale_Lastprocessed_Item_Label.Text = "test";
    // etc...
}

当您从表单调用它时,您会将引用传递给表单。如果从表单本身调用,可能就是这样。基本上,您需要帮助器类/方法来了解它使用的表单。

您需要将Form1类的对象传递到UpdateIntellisale方法中。 比如:

然后像这样称呼它:UpdateIntellisalethis当然,只有当这是Form1类时才有以下几个问题: 1.您对标签进行寻址,就像它被定义为Form1的静态成员一样。 请改用实例名。e、 g:

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.
见:

标签通常在设计器文件中定义为私有。 简单的方法是将它们声明为公共的,但是直接从其他类中寻址字段并不是一件好事。改为使用属性。
它不能工作。您调用类的类型Form1,而不是该类的实例!我试过了,但还是说没有定义found@JulianBechtold:您具体尝试了什么,错误具体是什么?在你的问题中包括代码而不是代码的图片,而是实际的代码。@JulianBechtold:确切的错误消息是什么?还有,房产是公共的吗?
class UiUpdate {
    public void UpdateIntellisale(Form1 form) {
        form.Intellisale_Lastprocessed_Item_Label.text = "test";
    }
}
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.