开发者

How to remove a tabpage from a context menu

开发者 https://www.devze.com 2023-02-10 13:28 出处:网络
I have written the code to display a context menu on right click of my tabpages. How would I go about actually removing the tabpage when the user clicks \"Remove Tab\" from the context menu? I have go

I have written the code to display a context menu on right click of my tabpages. How would I go about actually removing the tabpage when the user clicks "Remove Tab" from the context menu? I have gotten this far. (unloadProfile is my context menu item). I am unsure how to get the tabpage the context menu is associating with to remove it. Any help is appreciated.

// My Context Menu
private void t开发者_StackOverflowabControl_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            // iterate through all the tab pages
            for (int i = 0; i < tabControl.TabCount; i++)
            {
                // get their rectangle area and check if it contains the mouse cursor
                Rectangle r = tabControl.GetTabRect(i);
                if (r.Contains(e.Location))
                {
                    // show the context menu here
                    this.contextMenuStrip1.Show(this.tabControl, e.Location);
                }
            }
        }
    }

// Context menu click event
private void unloadProfile_Click(object sender, EventArgs e)
    {
        // iterate through all the tab pages
        for (int i = 0; i < tabControl.TabCount; i++)
        {

        }
    }


I don't think this is a correct way to do this, but it works.

In the tabControl1_MouseClick(object sender, MouseEventArgs e) event, set the Tag property of menustrip to the TabPage selected.

// show the context menu here
this.contextMenuStrip1.Tag = this.tabControl1.TabPages[i];
this.contextMenuStrip1.Show(this.tabControl1, e.Location);

And in the removeTabToolStripMenuItem_Click(object sender, EventArgs e) event remove the Tab Page using Tag property

this.tabControl1.TabPages.Remove(this.contextMenuStrip1.Tag as TabPage);

A null check will be good :) Hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消