C# 访问其他 Windows 窗体类中的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12042303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 20:11:46  来源:igfitidea点击:

Accessing variables in other Windows Form class

c#windowsforms

提问by John

I will appreciate if anyone can help me on this.

如果有人可以帮助我,我将不胜感激。

I have a windows form app that has three forms: form1, form2, form3. form1 starts when the app is activated. on form1, there is a button that brings up form2, and hide form1. there is also one button that brings up form3 and hides form2 on form2.

我有一个 windows 窗体应用程序,它具有三种窗体:form1、form2、form3。form1 在应用程序激活时启动。在form1 上,有一个按钮可以调出form2 并隐藏form1。还有一个按钮可以调出 form3 并隐藏 form2 上的 form2。

public partial class Form1 : Form
{

    Form2 f2= new Form2();
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();            
        f2.Show();        
    }
}


public partial class Form2 : Form
{
            Form3 f3 = new Form3();
    private void button1_Click(object sender, EventArgs e)
    {
         this.Hide();
         f3.Show();                
    }
 }

The question is on form3, i tried to access some of the variables that are assigned with values on runtime in form2. I think since i make f2 as modaless form, i should be able to access by simply using f2.myvariables, but the intellisense does not give me f2 object. Why is that? I found a way to declare those variables public static, so i could access by using form2.myvariables..Here is another thing that confuses me. Since all the values are assigned during runtime, how could static variable do this? I am a newbie on C#, and i already did a lot of searches on this, but seems no place answers my question exactly. Thanks for help in advance!!

问题是在form3上,我试图访问一些在form2中的运行时赋值的变量。我认为由于我将 f2 作为无模式形式,我应该能够通过简单地使用 f2.myvariables 来访问,但是智能感知并没有给我 f2 对象。这是为什么?我找到了一种将这些变量声明为 public static 的方法,因此我可以使用 form2.myvariables 进行访问。这是另一件让我感到困惑的事情。由于所有值都是在运行时分配的,静态变量如何做到这一点?我是 C# 的新手,我已经对此进行了大量搜索,但似乎没有地方能准确回答我的问题。提前感谢您的帮助!!

采纳答案by Servy

So you have information in the parent form (form2) that you want to access in a method of the child form (form3).

因此,您在父表单 (form2) 中拥有要在子表单 (form3) 的方法中访问的信息。

  1. Create properties in form3for the information that it will need.
  2. When form2creates an instance of form3it should set those properties.
  1. form3为它需要的信息创建属性。
  2. form2创建的实例form3,应该设置这些属性。

You should think of this not as having the child form ask for information from it's parent, but rather that the parent is giving information to its child. If you shift your mindset accordingly the code becomes not only easier to write, but also will be more in line with good coding practices (lower coupling, not exposing more information externally than needed, etc.)

您应该认为这不是让子表单向其父表单询问信息,而是父表单向其子表单提供信息。如果你相应地改变你的思维方式,代码不仅变得更容易编写,而且更符合良好的编码实践(更低的耦合,不向外部暴露比需要的更多的信息等)

To create a property you can do something like this in form3:

要创建属性,您可以在以下内容中执行以下操作form3

//TODO: give real name; adjust type as needed
public string SomePropertyName { get; set; }

then in form2you can do:

然后form2你可以这样做:

f3.SomePropertyName = "hello from form2";

or

或者

f3.SomePropertyName = someVariableInForm2;

回答by Brian S

If you have made the variables in question public on Form2, then your issue is that you've also made them static. When you define them as static, you are placing them on the type (Form2) not on the instance (f2).

如果您已在 Form2 上公开了有问题的变量,那么您的问题是您也已将它们设置为static。当您将它们定义为 时static,您将它们放置在类型 (Form2) 上而不是实例 (f2) 上。

Remove the static from the variable declaration and they should appear in intellisense for f2.

从变量声明中删除静态,它们应该出现在 f2 的智能感知中。

回答by perilbrain

I think since i make f2 as modaless form, i should be able to access by simply using f2.myvariables, but the intellisense does not give me f2 object. Why is that?

我认为由于我将 f2 作为无模式形式,我应该能够通过简单地使用 f2.myvariables 来访问,但是智能感知并没有给我 f2 对象。这是为什么?

Once you create instance of a class all the variables and methods declared as public should be available.Just recheck if you have declared your variables as public.

创建类的实例后,所有声明为 public 的变量和方法都应该可用。只需重新检查您是否已将变量声明为public.

Since all the values are assigned during runtime, how could static variable do this?

由于所有值都是在运行时分配的,静态变量如何做到这一点?

No, Static variables and methods are defined with the start of the program.They dont need instances to be created to refer them.

不,静态变量和方法是在程序开始时定义的。它们不需要创建实例来引用它们。

回答by Rodrigo Reis

Man,

男人,

Try to create an overload of the constructor method of Form3, passing variable values ??from form2 as method arguments.

尝试创建 Form3 的构造函数方法的重载,将来自 form2 的变量值作为方法参数传递。