C# 无法访问来自父 Class 的表单的 public 成员
C# Can Not Access public member of Form from Parent Class
我正在尝试在 Class 中添加一个事件处理程序,它引用在 Class 中实例化的表单控件的事件。所有 classes 都存在于同一个命名空间中。
该程序基于 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)
{
...
}
...
}
并且从设计者生成的 AppForm.Designer.cs
中的 public partial class AppForm
中,我在其中制作了控制修饰符 public
并制作了 class 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
.
问题是您的 appForm
字段被声明为 Form
类型。 Form
class 没有名为 someToolStripMenuItem
的成员。您需要将字段声明为 AppForm
类型才能访问该类型的成员。
由于您声明它的方式,编译器认为 appForm
是 Form
而不是 AppForm
:
Form appForm;
尝试将声明更改为 AppForm appForm;
或将其转换为:
((AppForm)appForm).someToolStripMenuItem.Click += form_Click;
我正在尝试在 Class 中添加一个事件处理程序,它引用在 Class 中实例化的表单控件的事件。所有 classes 都存在于同一个命名空间中。
该程序基于 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)
{
...
}
...
}
并且从设计者生成的 AppForm.Designer.cs
中的 public partial class AppForm
中,我在其中制作了控制修饰符 public
并制作了 class 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
.
问题是您的 appForm
字段被声明为 Form
类型。 Form
class 没有名为 someToolStripMenuItem
的成员。您需要将字段声明为 AppForm
类型才能访问该类型的成员。
由于您声明它的方式,编译器认为 appForm
是 Form
而不是 AppForm
:
Form appForm;
尝试将声明更改为 AppForm appForm;
或将其转换为:
((AppForm)appForm).someToolStripMenuItem.Click += form_Click;