MenuStrip中的枚举TabPages - Click事件

时间:2013-12-23 07:03:56

标签: c# winforms menu tabs

我正在将WinForms TabControl中的所有标签页显示到MenuStrip中。

填充菜单非常简单但是我似乎无法在选择时让事件处理程序将焦点设置为TabPage。

由于表格是动态创建的,因此它们没有对象名称。因此,在menu_click事件中,行tabEditor.SelectedTab = tabpage;没有意义。

如何链接menu_click事件以显示所选标签?

    private void pagesToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
    {
        //first clear all items already loaded
        pagesToolStripMenuItem.DropDownItems.Clear();

        //now re-populate them
        foreach (TabPage t in tabEditor.TabPages)
        {
            //create a menu item
            ToolStripMenuItem menu = new ToolStripMenuItem(t.Text);


            //add an event handler
            menu.Click += new EventHandler(menu_Click);

            pagesToolStripMenuItem.DropDownItems.Add( menu );
        }
    }



    void menu_Click(object sender, EventArgs e)
    {
        ToolStripMenuItem menu = (ToolStripMenuItem) sender;

        //Now show the Tab - something like this !
        // 
        tabEditor.SelectedTab = tabpage;


    }

1 个答案:

答案 0 :(得分:1)

试试这个:

private void tabsToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
{
    ...
    //create a menu item
    ToolStripMenuItem menu = new ToolStripMenuItem(t.Text);
    //Associate a tab index with a menu item
    menu.Tag = t.TabIndex;
    ...
}

private void menu_Click(object sender, EventArgs e)
{
    ToolStripMenuItem menu = (ToolStripMenuItem)sender;
    //Use a tab index associated with a menu item to select a tab
    tabEditor.SelectedIndex = (int)menu.Tag;
}