0

我在自定义控件中声明了一个 ContextMenuStrip,我似乎无法找到一种方法让控件返回或设置 ContextMenuStrip 的项目集合。我试过这段代码,但它不起作用

class Class1 : Panel
{
    ContextMenuStrip _Menu = new ContextMenuStrip();
    public ToolStripItemCollection Items
    {
        get { return _Menu.Items; }
        set { _Menu.Items.AddRange(value); }
    }
}

我怎样才能做到这一点?

4

2 回答 2

0
// Set  
ContextMenuStrip menu = new ContextMenuStrip();
menu.Items.Add(new ToolStripMenuItem("Item1", aNiceImage, someFunction));
menu.Items.Add(new ToolStripMenuItem("Item2", alsoNiceImage, someOtherFunction));

//Get
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) 
{
   contextMenuStrip1.Items[3].Select();
}
于 2013-05-05T10:12:23.273 回答
0

考试这个:

public partial class Form1 : Form
{
    ContextMenuStrip cms = new ContextMenuStrip();

    public Form1()
    {
        InitializeComponent();
        //cms.Items[;
    }
    public ToolStripItemCollection ConItems
    {
        get
        {
            return cms.Items;
        }
        set
        {
            cms.Items.Clear();
            ToolStripItemCollection tsc=(ToolStripItemCollection)value;
            foreach (ToolStripItem tsi in tsc)
            {
                cms.Items.Add(tsi);
            }
        }
    }

}
于 2013-05-05T11:20:22.630 回答