如何从上下文菜单中删除选项卡页

发布于 2024-10-17 11:46:27 字数 1043 浏览 4 评论 0原文

我已经编写了代码来在右键单击我的选项卡页时显示上下文菜单。当用户从上下文菜单中单击“删除选项卡”时,我将如何实际删除选项卡页?我已经走到这一步了。 (unloadProfile 是我的上下文菜单项)。我不确定如何获取上下文菜单关联的标签页以将其删除。任何帮助表示赞赏。

// My Context Menu
private void tabControl_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 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 tabControl_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++)
        {

        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2024-10-24 11:46:27

我认为这不是正确的方法,但它确实有效。

在 tabControl1_MouseClick(object sender, MouseEventArgs e) 事件中,将 menustrip 的 Tag 属性设置为所选的 TabPage。

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

并在removeTabToolStripMenuItem_Click(object sender, EventArgs e)事件中使用Tag属性删除选项卡页

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

空检查会很好:)希望它有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文