简体   繁体   English

从c#winform中的另一个类调用标签文本

[英]invoking lable text from another class in c# winform

I am currently updating my Form with this code within the public partial class Form1 : Form 我目前正在public partial class Form1 : Form使用此代码更新public partial class Form1 : Form

this.Invoke((MethodInvoker)delegate
{
    Intellisale_Lastprocessed_Item_Label.Text = "example"
}

since I have tons of UI elements I would rather get all my ui update code into another class. 因为我有大量的UI元素,所以我宁愿将所有ui更新代码放入另一个类中。

Therefore I built the class "UiUpdate". 因此,我建立了“ UiUpdate”类。
unfortunately from within this class i receive the message: "Form 1 does not contain a definition for Intellisale_Lastprocessed_Item_Label" 不幸的是,从此类中我收到消息:“表格1不包含Intellisale_Lastprocessed_Item_Label的定义”

在此处输入图片说明

Probably I have overlooked something really easy but I have not found the answer yet. 可能我忽略了一些确实很简单的事情,但尚未找到答案。

EDIT: 编辑:

Due to the Suggestions I changed my code to the following: 由于这些建议,我将代码更改为以下内容:

class UiUpdate
{
    public void UpdateIntellisale(Form1 form)
    {
        form.Intellisale_Lastprocessed_Item_Label.text = "example";
    }
}

unfortunately i still receive the message that no definition for the lable is available 不幸的是,我仍然收到消息,没有标签的定义

Update 2: 更新2:

as mentioned, the lables were defined private within the designer 如前所述,标签在设计师内部定义为私有

You're making a static reference to the Form1 class , not to a particular instance of that class. 您正在static引用Form1 ,而不是对该类的特定实例 Pass the instance you need to your method: 将所需的实例传递给方法:

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

When you call it from your form you would pass it the reference to your form. 当您从表单中调用它时,会将其引用传递给表单。 Which may be this if called from the form itself. 如果从表单本身调用,可能是this Basically you need your helper class/method to know what form it's working with. 基本上,您需要您的帮助程序类/方法来知道它使用的是哪种形式。

You need to pass the object of Form1 class into UpdateIntellisale method. 您需要将Form1类的对象传递给UpdateIntellisale方法。 Like: 喜欢:

class UiUpdate {
    public void UpdateIntellisale(Form1 form) {
        form.Intellisale_Lastprocessed_Item_Label.text = "test";
    }
}

And call it like: UpdateIntellisale(this) (of course only if this is of Form1 class) 并像这样调用: UpdateIntellisale(this) (当然,仅当this属于Form1类时)

there are several issues: 1. You address your label as if it was defined as a static member of Form1. 存在几个问题:1.您将标签视为已定义为Form1的静态成员。 Use the instance name instead. 请改用实例名称。 eg: 例如:

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.

see: https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-make-thread-safe-calls-to-windows-forms-controls 请参阅: https : //docs.microsoft.com/zh-cn/dotnet/framework/winforms/controls/how-to-make-thread-safe-calls-to-windows-forms-controls

  1. Usually the labels are defined private in the designer file. 通常,标签在设计器文件中定义为私有。 The easy way is to declare them public, but it is not a good thing to address a field directly from some other class. 简单的方法是将它们声明为公共,但是直接从其他类访问字段不是一件好事。 Use a Property instead. 改用属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM