如何从 class 获取打开表单的引用

How to get reference of an open form from a class

一些简短的背景信息: 我已经编码 VB.Net 和 PHP 至少两年(自学),所以如果我的术语和编码习惯不是一流的,请原谅。我会根据您的意见改进!

好的,所以我目前有一个登录表单,它被设置为 Program.cs 中的启动对象。这是我创建登录表单的新实例并通过引用 FormInstances class(在 Misc.cs 中)传递它的地方。

using System;
using System.Windows.Forms;

namespace Blah{
    class Program{
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.SetCompatibleTextRenderingDefault(false);
            Application.EnableVisualStyles();

            var LoginFRM = new Login();
            LoginFRM.ShowDialog();

            if(LoginFRM.LoginSuccessful == true) {
                FormInstances.LoginForm = LoginFRM;
                Application.Run(new MainForm());
            }else{
                Application.Exit();
            } 
        }
    }
}

这是我在 Misc.cs 中的代码:

namespace Blah{
    public static class FormInstances {
        public static Form LoginForm;
    }

    public class Misc{
        FormInstances.LoginForm.txtTest.text = "test";
    }
}

但是我收到了这个错误:

'Form' 不包含 'txtText' 的定义并且没有扩展方法 'txtText' 可以找到接受类型 'Form' 的第一个参数(您是否缺少使用指令或程序集引用?)

如何修复此错误,是否有更简单的方法来实现我的目标?

基础 class 表单不包含名为 'txtText' 的文本框,您需要声明表单的确切类型,而不是通用基础 class

namespace Blah{
    public static class FormInstances {
        public static Login LoginForm;
    }

    public class Misc{
        FormInstances.LoginForm.txtTest.text = "test";
    }
}

附带说明一下,为什么需要存储整个登录表单?如果您只需要知道您的用户是否已登录,那么您可以只存储从 属性 LoginSuccessful

获得的布尔变量