如何访问其他类中的控件

我正在学习C#,我需要的是从其他类 (相同的命名空间) 访问Form上的控件

我知道这里有很多关于这个主题的post,但没有找到完全解决方案’愚蠢’,所以我在这里写了我想出来的,请告诉我 – 这是正确的方法吗?

背景:我的应用程序中有一些“调试”表单,我需要所有其他表单才能将其活动记录到此表单中。 有一些ListBox控件,其中写入了来自其他表单的所有日志。 当我(或我的一个没有Visual Studio的测试人员朋友)玩app并且发生了一些不好的事情时,我可以查看该调试表单以查看所有详细的日志,这些事件发生在错误时刻之前。

我的主要forms的应用程序( frmMain ):

namespace myNamespace { public partial class frmMain : Form { private frmDebug debug; // frmDebug is declared in other class // we will hold reference to frmDebug form in 'debug' public frmMain() { // constructor of the main form 'frmMain' InitializeComponent(); debug = new frmDebug(); // we create new instance of frmDebug immediately when } // our main form is created (app started) and whole time // of running this app we can access frmDebug from // within frmMain through 'debug' variable // clicking button 'btnLoggingTest', the log is written // in the 'frmDebug' form even if it is closed (not visible) private void btnLoggingTest_Click(object sender, EventArgs e) { debug.Log("log this text for me please"); } // Click handler of the button 'btnShowDebug' : private void btnShowDebug_Click(object sender, EventArgs e) { debug.ShowDialog(); // here we can show frmDebug (in 'modal' style) } // just to see what log-information is written there } // frmMain class } // namespace 

这里是类frmDebug本身的代码🙁表单上只放置了一个Listbox)

 namespace myNamespace { public partial class frmDebug : Form { public frmDebug() { InitializeComponent(); } public void Log(string txt) { // this is the actual 'Log' function (or method) this.listBox1.Items.Add(txt); Application.DoEvents(); // if the logging takes place in some } // computing-intensive 'loop' or function, // (or in my case FTP login and upload process) // 'DoEvents' ensures every log appears immediately // after the 'Log()' was called. Otherwise all logs // would appear together at once, as soon as the // computing-intensive 'loop' is finished } // class frmDebug } // namespace 

我的胃里有一种奇怪的感觉我做错了所以请告诉我如何正确地做到这一点 🙂
如果没关系,希望它可以帮到像我这样的人。

谢谢 !

您的应用程序可能有一个名为“Program”的类。 在那里你会找到代码

 var mainForm = new frmMain(); Application.Run(frmMain); 

在此类中为调试表单创建静态属性

 public static frmDebug DebuggingForm { get; private set; } 

像这样更改启动代码

 DebuggingForm = new frmDebug(); var mainForm = new frmMain(); Application.Run(frmMain); 

从其他课程中,您可以像这样访问此表单

 Program.DebuggingForm.Log("log this text for me please"); Program.DebuggingForm.Show(); 

我认为你不必在内存中保持调试forms。 您可以将日志写入某个对象。 例如静态日志:

 public static Log { private static List _messages = new List(); public static Write(string message) { _messages.Add(message); } public static IEnumerable Messages { get { return _messages; } } } 

您可以通过应用程序的每个点添加日志消息

 Log.Write("log this text for me please"); 

如果您需要查看这些消息,只需创建并显示调试表单:

 private void btnShowDebug_Click(object sender, EventArgs e) { using (frmDebug debug = new frmDebug()) debug.ShowDialog(); } 

在加载的调试表单中,将Log.Messages分配给列表框。

一种不同的方法是使用一个Event Sink作为调试信息的发布订阅中心,这样你就不会在整个地方依赖调试表单,例如:

 public class EventSink { private static readonly IList> _listeners = new List>(); public static void RegisterListener(Action listener) { _listeners.Add(listener); } public static void RaiseEvent(string message) { foreach (var l in _listeners) l(message); } } 

在你的frmDebug的构造函数中,你会做:

 EventSink.RegisterListener(msg=>listBox1.Items.Add(msg)); 

每次需要向调试控制台添加消息时,您都会执行以下操作:

 EventSink.RaiseEvent("this is a debug message"); 

这样你就可以注册新的监听器来做不同的事情,比如在某些特定事件发生时给你发电子邮件等等。你没有加上你的调试表格(脱钩很好:)