我正在开发一个应用程序,它需要随着时间的推移关闭和打开不同的 winform。我遇到了一个问题,如何传递一个变量,在这种情况下是一个以初始形式打开的 SerialPort?
从 Program.cs 打开表单
Application.Run(new Auth());
Application.Run(new Main());
Auth.cs 代码包括:
SerialPort RCU = new SerialPort();
RCU.PortName = textbox.Text; //Port name is get from user input
我想以另一种形式“Main”使用这个端口。
有没有可能做到这一点?
谢谢
只需Auth
在Main
构造函数中请求一个对象,然后按该顺序初始化它们:
Auth auth = new Auth();
Main main = new Main(auth);
Application.Run(auth);
Application.Run(main);
然后,Main
您可以保存该对象并在设置后获取该属性:
public Main(Auth auth)
{
AuthWindow = auth;
}
public Auth AuthWindow {get; set;}
// later..
AuthWindow.RCU
但是请注意,两次Application.Run
调用不是您想要的方式,只需创建一个新Auth
表单并在加载主表单时显示它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句