与鼠标中键的WinForms标签控件关闭选项卡 [英] Close tab on winforms tab control with middle mouse button

查看:193
本文介绍了与鼠标中键的WinForms标签控件关闭选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有简单的(5行代码)的方式来做到这一点?

Is there any easy (5 lines of code) way to do this?

推荐答案

最短的代码删除标签被点击是使用LINQ鼠标中键。

The shortest code to delete the tab the middle mouse button was clicked on is by using LINQ.

this.tabControl1.MouseClick += tabControl1_MouseClick;



和为处理程序本身

And for the handler itself

private void tabControl1_MouseClick(object sender, MouseEventArgs e)
{
  var tabControl = sender as TabControl;
  var tabs = tabControl.TabPages;

  if (e.Button == MouseButtons.Middle)
  {
    tabs.Remove(tabs.Cast<TabPage>()
            .Where((t, i) => tabControl.GetTabRect(i).Contains(e.Location))
            .First());
  }
}



如果你是追求最少行的数量,在这里它是在一行

And if you are striving for least amount of lines, here it is in one line

tabControl1.MouseClick += delegate(object sender, MouseEventArgs e) { var tabControl = sender as TabControl; var tabs = tabControl.TabPages; if (e.Button == MouseButtons.Middle) { tabs.Remove(tabs.Cast<TabPage>().Where((t, i) => tabControl.GetTabRect(i).Contains(e.Location)).First()); } };

这篇关于与鼠标中键的WinForms标签控件关闭选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆