简体   繁体   English

在另一个类/表单中调用非静态方法

[英]Call a non-static method in another Class/Form

I'd like call a non static method in an another class. 我想在另一个类中调用非静态方法。 The method what i want call change the own Windows Form controls Text properties. 我想要调用的方法改变了自己的Windows窗体控件的Text属性。 It's in code: 它在代码中:

private void Valtas_angolra()
{
    //Angol kultúra értékül adása a 'cul' változónak.
    cul = CultureInfo.CreateSpecificCulture("en-US");

    //Egyes elemek 'Text' tulajdonságainak beállítása a 'Res.en.resx' fájlból.
    this.Text = res_man.GetString("Termekek_kezelese_From", cul);
    Termek_adatok_Groupbox.Text = res_man.GetString("Termek_adatok_Groupbox", cul);
    Termekkod_Label.Text = res_man.GetString("Termekkod_MIND_Label", cul);
    Termeknev_Label.Text = res_man.GetString("Termeknev_MIND_Label", cul);
}

How you can see the method get the Texts from a .resx file. 如何看待该方法从.resx文件中获取文本。

Now i'd like this function call in an another class, for example: 现在我想在另一个类中调用此函数,例如:

namespace EcoHelp
{
    class Kozos_fuggvenyek
    {
         public static void call_nonstatic()
         {
               //calling here
         }
     }
}

I know that I can with a new instance, but here comes the problem, because the Form where the "Valtas_angolra" method is declared, already opened. 我知道我可以使用一个新实例,但问题就出现了,因为声明了“Valtas_angolra”方法的Form已经打开了。 So i need use the old instance if it's possible. 如果可能的话,我需要使用旧实例。

If you need more details just ask. 如果您需要更多细节,请询问。

Pass your static method the instance of the form you want to change. 将静态方法传递给要更改的表单实例。

EDIT: 编辑:

class Kozos_fuggvenyek
    {
         public static void call_nonstatic(Form yourForm)
         {
               //Do what you want to your form.
         }
     }

and then you call it like this: 然后你这样称呼它:

 private void Valtas_angolra()
    {
          Kozos_fuggvenyek.call_nonstatic(this);
    }

Your Valtas_angolra() function is marked as private, so even if you got an instance of your form, you won't be able to call it in this other class. 你的Valtas_angolra()函数被标记为私有,所以即使你有一个表单实例,你也无法在另一个类中调用它。 If you change it to public, it should be able to, once you have an instance of the form. 如果您将其更改为公开,那么一旦您拥有该表单的实例,它应该能够。

Try giving your current function access to the form by passing it through an added parameter. 尝试通过添加参数传递给当前函数访问表单。

You can try this: 你可以试试这个:

namespace EcoHelp
{
    class Kozos_fuggvenyek
    {
        public static void call_nonstatic(Form form)
        {
           //Angol kultúra értékül adása a 'cul' változónak.
           var cul = CultureInfo.CreateSpecificCulture("en-US");

           //Egyes elemek 'Text' tulajdonságainak beállítása a 'Res.en.resx' fájlból.
           form.Text = form.res_man.GetString("Termekek_kezelese_From", cul);
           form.Termek_adatok_Groupbox.Text = form.res_man.GetString("Termek_adatok_Groupbox", cul);
           form.Termekkod_Label.Text = form.res_man.GetString("Termekkod_MIND_Label", cul);
           form.Termeknev_Label.Text = form.res_man.GetString("Termeknev_MIND_Label", cul);
        }
    }
}

You have to expose these fields as public properties inside the class of Valtas_angolra() 您必须将这些字段公开为Valtas_angolra()类中的公共属性

  • Termek_adatok_Groupbox Termek_adatok_Groupbox
  • Termekkod_Label Termekkod_Label
  • res_man res_man

Then call it like this: 然后像这样称呼它:

private void Valtas_angolra()
{
    Kozos_fuggvenyek.call_nonstatic(this);
}

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

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