How to close a tab with mouse middle click?
When I had to do this I’ve searched all over the Internet for some information on how to implement such a feature, but I have found nothing.
The main problem was how to detect on which tab exactly has the user clicked on. I haven’t found any method or property of the TabControl or TabPage controls in .NET Framework 2.0 that will be at some help. But after some experimenting I’ve found a very easy and effective way to do this. Here is the code:
VB:
Private Sub TabControl1_MouseUp(ByVal sender As Object _
, ByVal e As System.Windows.Forms.MouseEventArgs _
) Handles TabControl1.MouseUp
Dim nTabIndexAs Integer
'Detect on which tab the user has clicked
For nTabIndex= 0 To TabControl1.TabPages.Count - 1
If e.X >= TabControl1.GetTabRect(nTabIndex).X _
AndAlso e.X <= TabControl1.GetTabRect(nTabIndex).Width _
+ TabControl1.GetTabRect(nTabIndex).X _
Then
Exit For
End If
Next
If e.Button = Windows.Forms.MouseButtons.Middle Then
TabControl1.TabPages.RemoveAt(nTabIndex)
End If
End Sub
Leave a Reply
Want to join the discussion?Feel free to contribute!