我正在学习C#,我需要的是对其他类(同一名称空间)的窗体上的访问控制。如何访问其他类中的控件
我知道这里有很多关于这个主题的文章,但是没有找到完整的解决方案,所以我在这里写下我想到的,请告诉我 - 这是正确的方法吗?
背景:我在应用程序中有一些“调试”形式,我需要所有其他形式才能将其活动记录到此表单中。有一些ListBox控件,其中所有来自其他表单的日志都被写入。当我(或没有Visual Studio的测试者朋友之一)玩弄应用程序并发生不好的事情时,我可以查看该调试窗体以查看所有详细日志在'错误时刻'之前发生的情况。
我的应用程序的主要形式(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本身: (只有一个列表框放置在窗体上)
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
我肚子里有一种奇怪的感觉我正在做这一切都是错误的所以请告诉我如何正确地做到这一点 :)
如果没关系,希望它可以帮助像我这样的人。
谢谢!
如果您发现自己经常从他们没有定义的表单中访问控件,很有可能你的UI架构不健全。我建议你阅读MVC和MVVM(维基百科文章是很好的介绍),以了解现代UI交互模式。 – 2012-03-22 23:24:35