我想獲得一個組合框也像combobox1.Items.Add()// editor.Tags.Tags1()c#嵌套類和可見性?
像這樣相同的功能:
class Main()
{
// The editor is passed into the editor class, that allows the editor class to update the editor.
MyEditorClass editor = new MyEditorClass(editorBox);
editor.Tags.InsertTag1();
// Now i don't want people to do this
// MyEditorClass.TagClass tags = new MyEditorClass.TagClass();
}
的原因是,標籤類調用傳遞給MyEditorClass的editorBox,如果你創建一個沒有該編輯器的標籤類,它將不起作用。
我MyEditorClass看起來是這樣的:
public class MyEditorClass
{
static RichTextBox editor;
public TagClass Tags;
public MyEditorClass(RichTextBox editorBox)
{
editor = editorBox;
Tags = new TagClass();
}
public class TagClass
{
public void InsertTag1()
{
editor.Text += "Test tag 1";
}
}
}
我試圖讓TagClass靜態的,但沒有奏效。組合框的結構如何?既然你不能使用Combobox.Items,但是如果你聲明瞭一個,你可以在你聲明的那個上使用Combobox.Items。
問題是,我沒有想法如何建立我的班級以反映組合框架。 (tagsclass也必須有權訪問編輯器)。我希望我的所有標籤都位於名爲tags的方法/屬性中。這使它更易於使用。正常的MyEditorClass也包含保存,打開,新建等元素。所以如果有更好的方法來構建它,請告訴我=) – Patrick 2010-11-30 09:53:09
好吧,現在我明白你需要什麼了。答案已更新。 – NOtherDev 2010-11-30 10:02:41