我想在类中引用一个事件处理程序,该类指向在该类中实例化的表单控件的事件。所有的类都存在于同一个名字空间中。C#不能从父类访问表单的公共成员
该程序基于ApplicationContext
表单应用程序。在static void Main()
在Program.cs
CustomApplicationContext applicationContext = new CustomApplicationContext();
Application.Run(applicationContext);
内public class CustomApplicationContext
public class CustomApplicationContext : ApplicationContext
{
//create the application form
Form appForm;
public CustomApplicationContext()
{
InitializeContext();
//create instance of appForm
appForm = new AppForm();
//subscribe event handler to form closing event
appForm.FormClosing += form_FormClosing; //this works fine
//subscribe event handler to form control click event
appForm.someToolStripMenuItem.Click += form_Click; //doesn't compile
//can't even find appForm.someToolStripmenuItem in code completion!
}
void form_FormClosing(object sender, FormClosingEventArgs e)
{
...
}
void form_Click(object sender, EventArgs e)
{
...
}
...
}
而且从public partial class AppForm
内AppForm.Designer.cs
这是由设计师,在那里我做了控制修改public
产生的,我所做的类public
public partial class AppForm //note that I made this public
{
...
this.someToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
...
//
// someToolStripMenuItem
//
this.someToolStripMenuItem.Name = "someToolStripMenuItem";
this.someToolStripMenuItem.Size = new System.Drawing.Size(178, 22);
this.someToolStripMenuItem.Text = "Some Item";
...
public System.Windows.Forms.ToolStripMenuItem someToolStripMenuItem;
}
关于电子是什么我是否做错了?当我输入appForm.
时,someToolStripMenuItem
甚至不会出现在代码完成框中,就好像它在上下文中不可访问 - 但appForm
可访问,并且someToolStripMenuItem
是public
。
所以班级是公开的,但也是会员公众?小写名称通常指出非公共变量。 – Wormbo
感谢您的提示,我会在我的代码中进行更新。 – khargoosh