简体   繁体   English

更改静态无效C#中的标签文本

[英]Change label text in Static void C#

I want change the text of a label on one form to the text of a button on another form when I press the button. 我想在按下按钮时将一种形式的标签文本更改为另一种形式的按钮文本。

To do this I have created this on the form where the label is 为此,我在标签所在的表单上创建了此标签

public static void changeText(string text)
{
     L1.text = text;
}

this code is on the form with the button 该代码在带有按钮的表单上

mainForm.changeText(this.Text);

It then gives the error : An object reference is required for the non-static field, method, or property... 然后给出错误: 非静态字段,方法或属性需要对象引用。

This may seem like a stupid question but I am still new to C# so please help me. 这似乎是一个愚蠢的问题,但是我对C#还是陌生的,所以请帮助我。

About static and non-static members 关于静态和非静态成员

There are two kinds of type members: non-static and static. 类型成员有两种:非静态成员和静态成员。 Non-static members are also called instance members, because they appear in the object instances of the type. 非静态成员也称为实例成员,因为它们出现在类型的对象实例中。 Static members are bound to the type itself, and not its object instances, so you can use them without actually instantiating the type. 静态成员绑定到类型本身,而不是对象实例,因此您可以在不实际实例化类型的情况下使用它们。

Consider the following: 考虑以下:

class MyClass
{
      // static member: can NOT reference 'this', as it is not in the context of an object instance of the type
      public static bool IsTrue()
      {
           return true;
      }

      // constructor: this runs whenever the type is instantiated
      public MyClass()
      {

      }

      // instance member: can access to 'this', which references the context object instance of the type
      public int GetNumber()
      {
           return 42;
      }
}

You can use the above type as following: 您可以按以下方式使用以上类型:

if(MyClass.IsTrue()) // static call
{
    var myObject = new MyClass(); // constructor call
    int result = myObject.GetNumber(); // instance member call
    Console.WriteLine(result);
}

On to your specific problem... 关于您的特定问题...

If you're perfectly sure that you need that logic inside a static method, you'll need to get an object instance of the form you wish to change. 如果您完全确定需要在静态方法中使用该逻辑,则需要获取要更改的表单的对象实例。 Unfortunately singletons don't work very well, because the VS designer needs to create an object instance of your Form, which obviously violates the singleton pattern. 不幸的是,单例无法很好地工作,因为VS设计器需要创建Form的对象实例,这显然违反了单例模式。

What you can still use, is (in case of a Windows Forms application): Application.OpenForms . 对于Windows Forms应用程序,您仍然可以使用Application.OpenForms This returns a read-only collection that contains all currently open forms of the application. 这将返回一个只读集合,其中包含应用程序的所有当前打开的表单。 You can use this to find the object instance of the form you want to change, and then perform that change. 您可以使用它来查找要更改的表单的对象实例,然后执行该更改。

Be advised that if this is a multi-threaded situation (ie the static method runs in a different thread than the GUI thread), you'll have to use some sort of synchronization mechanism, such as InvokeRequired and Invoke() . 请注意,如果是多线程情况(即,静态方法在不同于GUI线程的线程中运行),则必须使用某种同步机制,例如InvokeRequiredInvoke()

您不想为此使用静态方法,因为L1是mainForm类的成员。

L1 is not static, so you cant have a static function interacting with it. L1不是静态的,因此您不能具有与之交互的静态函数。 Having a static let you able to write something like MainForm.changeText(...) , but in this case what is L1 ? 有了静态变量,您就可以编写MainForm.changeText(...)类的东西,但是在这种情况下,L1是什么? I think we can say: 我想我们可以说:

  1. You dont need a function to change the label text, the property Text is already written 您不需要更改标签文本的功能,属性Text已被写入
  2. If there is some logic needed to mangle the tet before you can: 如果需要一些逻辑来处理tet,则可以:

Consider if the function you need is so general that can apply to many labels across your app, in this case an extension method would be good. 考虑一下您所需的功能是否如此通用,可以在您的应用程序中应用于许多标签,在这种情况下,扩展方法会很好。 In other case if you want a function in the main form to set the text somewhere, and this place can change, or the text need some mangling, a member function would be good, and probably a DataBinding would be better. 在其他情况下,如果希望主窗体中的函数将文本设置在某个地方,并且此位置可以更改,或者文本需要进行一些修饰,则成员函数会很好,而DataBinding可能会更好。

The error means your static function is accessing a non-static variable (control L1). 该错误表示您的静态函数正在访问非静态变量(控件L1)。

Static functions can only access to static variables. 静态函数只能访问静态变量。 You can change L1 to static variable to make it work. 您可以将L1更改为静态变量以使其起作用。

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

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