只是一些简短的背景信息:我已经编写 VB.Net 和 PHP 至少两年(自学),所以如果我的术语和编码习惯不是一流的,请原谅。我会根据你的意见改进!
好的,所以我目前有一个登录表单,它在 Program.cs 中设置为启动对象。这是我创建登录表单的新实例并通过引用 FormInstances 类(在 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”的定义,并且找不到接受“Form”类型的第一个参数的扩展方法“txtText”(您是否缺少 using 指令或程序集引用?)
我该如何解决这个错误,有没有更简单的方法来实现我的目标?