本文介绍了如何从另一种形式访问一种形式的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个WinForms,在 Form1 中,我声明了这一点:

I have 2 WinForms, in Form1 I declared this:

public int NumberOfContacts { get; set; }

我需要从 Form2 访问该属性.

推荐答案

这是一个有关从另一个表单中加入Form成员的常见问题,我假设您要从Form1的当前活动实例中访问NumberOfContacts,在那样的话,您就可以简单地完成,就像您在自己的声明中所做的一样:

This is a usual question concerning acceding a Form's member from an other form, I will assume that you want to access NumberOfContacts from the currect active instance of Form1, in that case you would simply , and just as you did declare in your :

Form1 :

 public partial class Form1 : Form
    {
        public int NumberOfContacts { get; set; }
        public Form1()
        {
            InitializeComponent();
        }
    }

Form2 中:

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            // ActiveForm will give you access to the current opened/activated Form1 instance
            var numberOfContacts  = ((Form1) Form1.ActiveForm).NumberOfContacts;
        }
    }

这应该有效.

这篇关于如何从另一种形式访问一种形式的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 11:18