我想在類中引用一個事件處理程序,該類指向在該類中實例化的表單控件的事件。所有的類都存在於同一個名字空間中。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