【发布时间】:2012-02-22 08:37:56
【问题描述】:
我有一个名为 myForm 的 winform 应用程序。在这个表单中,我打开另一个表单:
private OtherForm otherForm; //this is a field
private OpenOtherForm()
{
if (otherForm == null)
{
otherForm = new OtherForm();
otherForm.FormClosing += delegate { MessageBox.Show("OtherForm will be closed"); otherForm = null};
}
MessageBox.Show("Form is already active!");
}
这很好用。但我也有第二种形式的一些方法。我想尝试捕捉他们的电话。
例如,如果在第二个表单中调用 OtherForm.DoSomething(),我想要一个消息框来显示。
我尝试分配OtherForm.DoSomething() += delegate { /* mesagebox */}; 但这不能编译
【问题讨论】:
-
编译时收到的错误信息是什么??
-
您确实将 DoSomething 声明为事件,对吧?事件也没有括号,这可能是原因。请改用
OtherForm.DoSomething += delegate { };。 -
不,DoSomething 只是一个返回字符串的普通方法。
标签: c# winforms events delegates