C#的winform:访问其他形式放的公共属性;静态和公共属性的区别 [英] C# winform: Accessing public properties from other forms & difference between static and public properties

查看:209
本文介绍了C#的winform:访问其他形式放的公共属性;静态和公共属性的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解什么静态和公共属性之间的区别。但是当我试图访问其他形式的我的公共财产测试它说,'空'

I am trying to understand whats the difference between a static and public properties. But when I tried to access my public property 'Test' in other form it says 'null'.

继承人Form1中:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private string _test;

    public string Test
    {
        get { return _test; }
        set { _test = value; }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        _test = "This is a test";
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.Show();
    }
}

下面是窗体2:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        Form1 frm1 = new Form1();
        label1.Text = frm1.Test;
    }
}

要检查测试在Form1中的价值,我把一个断点这一行:

To check the value of 'Test' in Form1, I put a breakpoint to this line:

label1.Text = frm1.Test;



但值是'空'。

But the value is 'null'.

请帮助我,我怎么能访问公共属性其他形式。

Please help me how can I access public properties to other forms.

和BTW我试图使这个公共财产是一个公共静态。我可以访问此使用这个:

And BTW I tried to make this public property be a 'public static'. I can access this using this:

Form1.Test

不过,我注意到,我可以改变从窗体2测试值,我不希望发生的。这就是为什么我想使用公共财产,但没有运气。有人能解释一下我这些事情。感谢您的所有帮助家伙!

But I noticed that I can change 'Test' value from Form2 which I don't want to happen. That's why I am trying to use public property but with no luck. Can somebody clarify me these things. Thanks for all your help guys!

EDIT: (For follow up question) 

约翰·克尔纳先生的回答是我的问题的最佳答案。但我有一个跟进的问题,我试图让这些测试性能是一个'静',我注意到,即使我做这个属性静态或公共财产,它仍然可以在窗体2编辑。为了让自己在这里清楚的一个示例:

Sir John Koerner's answer is the best answer for my question. But I have a follow up question, I tried to make these 'test' properties to be a 'static', and I noticed that even if I make this property a static or public property, it still can be edit in Form2. To make myself clear here's a sample:

public partial class Form2 : Form
{
    private Form1 f1;
    public Form2(Form1 ParentForm)
    {
        InitializeComponent();
        f1 = ParentForm;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        label1.Text = f1.Test;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        f1.Test = "This test has been changed!";

        this.Close();
    }
}



窗体2关闭后,我试图在Form1_Load的再次突破检查测试的价值,它被改变!如何使在Form1上公共财产为readOnly在2并不能editted?请澄清我。非常感谢你们!

After Form2 closed, I tried to break again in Form1_Load to check value of 'Test', and it was changed! How can I make a public property in Form1 to readOnly in Form2 and cannot be editted? Please clarify to me. Thanks a lot guys!

推荐答案

您属性是一个实例变量,所以值可以是跨<$ C的不同实例不同$ C> Form1中。

Your property is an instance variable, so the value can be different across different instances of Form1.

如果你正试图从一个父窗体访问实例变量,要做到这一点最简单的方法是通过Form1中在到窗体2的构造

If you are trying to access instance variables from a parent form, the easiest way to do that is to pass Form1 in to the constructor of Form2.

public partial class Form2 : Form
{
    private Form1 f1;
    public Form2(Form1 ParentForm)
    {
        InitializeComponent();
        f1 = ParentForm;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        label1.Text = f1.Test;
    }
}



然后,当你创建Form1中一个新的窗体2,你能做到这一点:

Then when you create a new Form2 from Form1, you can do this:

Form2 frm2 = new Form2(this);

如果你想成为只读你的财产,你根本无法指定setter方法​​:

If you want your property to be read only, you can simply not specify a setter:

public string Test
{
    get { return _test; }
}

这篇关于C#的winform:访问其他形式放的公共属性;静态和公共属性的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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