访问其他Windows窗体类中的变量 [英] Accessing variables in other Windows Form class

查看:63
本文介绍了访问其他Windows窗体类中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I will appreciate if anyone can help me on this.

我有一个Windows窗体应用程序,该应用程序具有三种形式:form1,form2,form3。激活应用程序后,form1启动。在form1上,有一个按钮调出form2,并隐藏form1。

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.

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();                
    }
 }

问题在form3上,我尝试访问一些在form2的运行时中分配了值的变量。我认为,因为我将f2设为模态形式,所以我应该能够仅通过使用f2.myvariables进行访问,但是智能感知不会给我f2对象。这是为什么?我找到了一种将这些变量声明为公共静态变量的方法,因此我可以使用form2.myvariables进行访问。这是另一件事,使我感到困惑。由于所有值都是在运行时分配的,静态变量该怎么办?我是C#的新手,对此我已经进行了很多搜索,但是似乎没有地方可以准确地回答我的问题。

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!!

推荐答案

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

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


  1. form3 中创建属性

  2. form2 创建 form3 的实例时它应该设置这些属性。

  1. Create properties in form3 for the information that it will need.
  2. When form2 creates an instance of form3 it should set those properties.

您应该认为这不是让孩子表单向其父级询问信息,而是父母正在向其子女提供信息。如果您改变思维方式,那么代码不仅变得更容易编写,而且将更符合良好的编码习惯(耦合度更低,不会在外部暴露过多的信息,等等)。

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.)

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

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

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

然后在 form2 中可以执行以下操作:

then in form2 you can do:

f3.SomePropertyName = "hello from form2";

f3.SomePropertyName = someVariableInForm2;

这篇关于访问其他Windows窗体类中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆