如何从上下文菜单中删除选项卡页
我已经编写了代码来在右键单击我的选项卡页时显示上下文菜单。当用户从上下文菜单中单击“删除选项卡”时,我将如何实际删除选项卡页?我已经走到这一步了。 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这不是正确的方法,但它确实有效。
在 tabControl1_MouseClick(object sender, MouseEventArgs e) 事件中,将 menustrip 的 Tag 属性设置为所选的 TabPage。
并在removeTabToolStripMenuItem_Click(object sender, EventArgs e)事件中使用Tag属性删除选项卡页
空检查会很好:)希望它有帮助。
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.
And in the removeTabToolStripMenuItem_Click(object sender, EventArgs e) event remove the Tab Page using Tag property
A null check will be good :) Hope it helps.