C# c从另一个类/事件更新WinForm GUI文本框
我正在学习使用PTGRY相机的API 下面是公司提供的关于设备到达和移除的类示例C# c从另一个类/事件更新WinForm GUI文本框,c#,winforms,desktop-application,C#,Winforms,Desktop Application,我正在学习使用PTGRY相机的API 下面是公司提供的关于设备到达和移除的类示例 Class SystemEventListener: ManagedInterfaceEvent { private IManagedSystem system; public SystemEventListener(IManagedSystem sys) { system = sys; } protected override void OnD
Class SystemEventListener: ManagedInterfaceEvent
{
private IManagedSystem system;
public SystemEventListener(IManagedSystem sys) { system = sys; }
protected override void OnDeviceArrival(UInt64 serialNumber)
{
int count = system.GetCameras().Count;
Console.WriteLine("System event listener:");
Console.WriteLine("\tThere {0} {1} {2} on the system.", (count == 1 ? "is" : "are"), count, (count == 1 ? "device" : "devices"));
}
protected override void OnDeviceRemoval(UInt64 serialNumber)
{
int count = system.GetCameras().Count;
Console.WriteLine("System event listener:");
Console.WriteLine("\tThere {0} {1} {2} on the system.", (count == 1 ? "is" : "are"), count, (count == 1 ? "device" : "devices"));
}
}
我正在努力适应我的制胜环境。在努力从这个类中更新GUI之后,我设法按照链接更新文本框。但是,它涉及修改program.cs文件
我的问题是如何在另一个类中从这些事件更新文本框,最好不要触摸Program.cs
我确实尝试过使用委托/事件等,但每次在我的主窗体Form1实例上都遇到空引用异常。显然我一定做错了什么
下面是我当前的实现,它可以工作,但我希望有另一种方法,而不是修改program.cs
Program.cs
public static Form1 MainForm;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm = new Form1();
Application.Run(MainForm);
}
表格1.cs
public Form1()
{
InitializeComponent();
SystemEventListener systemEventListener = new SystemEventListener(system);
system.RegisterInterfaceEvent(systemEventListener);
}
private void AppendTextBox(string value)
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(AppendTextBox), new object[] { value });
return;
}
textBoxCamProperties.AppendText(DateTime.Now.ToString("h:mm:ss tt") + "-" + value + Environment.NewLine);
}
public class SystemEventListener : ManagedInterfaceEvent
{
private IManagedSystem system;
public SystemEventListener(IManagedSystem sys) { system = sys; }
protected override void OnDeviceArrival(UInt64 serialNumber)
{
//int count = system.GetCameras().Count;
Program.MainForm.AppendTextBox("Device attached\r\n");
}
protected override void OnDeviceRemoval(UInt64 serialNumber)
{
//int count = system.GetCameras().Count;
Program.MainForm.AppendTextBox("Device removed\r\n");
}
}
多谢各位
更新:手袋螃蟹的荣誉!我可以使用他的方法而不必触摸Program.cs.:-
有人能告诉我在这种特殊情况下如何正确使用事件/委托吗
干杯,非常感谢 而不是:
Program.MainForm.AppendTextBox("Device removed\r\n");
使用:
您需要使用ref单词:阅读此链接 因此,在Form1的构造函数中,您可以参考如下程序:
public Form1(ref Program myProgram){
myProgram.MainForm.AppendTextBox("Device removed \r\n");
}
在Program.CS中,您可以添加以下内容:
static void Main()
{
Program myProgram = this;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm = new Form1(ref myProgram);
Application.Run(MainForm);
}
希望它有帮助:由于您使用的是已在构造函数中注册的接口,因此有可能在构造函数结束之前触发第一个事件,并且Program.MainForm中的值仍然为null。尝试只在表单的OnLoad函数中注册事件。嗨,Julo谢谢你的帮助。我确实尝试了Form1_加载事件,但没有效果。感谢您的帮助,但我否决了您的答案,因为它增加了混乱。您正在谈论将静态类作为引用传递,这是不可能的,并且会引发编译时错误。我还怀疑你误解了这个问题。A.Gerber,谢谢你的帮助。我试过了,正如Fixetion提到的,我遇到了一个错误。好的,很抱歉。祝你好运:非常感谢,手袋蟹。今天我学到了一些新东西。干杯-
public Form1(ref Program myProgram){
myProgram.MainForm.AppendTextBox("Device removed \r\n");
}
static void Main()
{
Program myProgram = this;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm = new Form1(ref myProgram);
Application.Run(MainForm);
}