Removing a Tab from a TabControl with mouse events

Arokh

Centurion
Joined
Apr 11, 2006
Messages
124
Hi
I'm having a bit of trouble with removing a tab from a tabcontrol.

The user can dynamically add tab, so obviously a way to close them is needed.
First I tried to somehow a button to the tablabel but that has proven to be a little difficult.
Now I just want to close a tab when the user makes a doubleclick on the tablabel.

But the doubleclick event doesn't tell me which tab was clicked,
it doesn't even tell me the mouse position.
The clicked event passes the mouse position but I still don't know which tab was clicked.

Any idea how I can realize this?
 
I found a mediocre solution on CodeProject. As far as I can see, the biggest problem is that there is no way to obtain the bounding rectangle for each tab unless you enable owner drawing, which means you lose visual styles.

[Edit]Looking around Google, there seem to be plenty of free custom-made tab controls that support close buttons, but you still lose the native OS appearance.[/edit]
 
Last edited:
Hi
I'm having a bit of trouble with removing a tab from a tabcontrol.

The user can dynamically add tab, so obviously a way to close them is needed.
First I tried to somehow a button to the tablabel but that has proven to be a little difficult.
Now I just want to close a tab when the user makes a doubleclick on the tablabel.

But the doubleclick event doesn't tell me which tab was clicked,
it doesn't even tell me the mouse position.
The clicked event passes the mouse position but I still don't know which tab was clicked.

Any idea how I can realize this?

You can add event handlers to your tabpages / tabcontrol dynamically as well
First I tried adding event handlers to the tabpages but this will only trigger when you actually click the page instead of the label. So here is another workaround:
I made a form with a tabcontrol called tabcontrol1
As you click the tab the selectedTab changes as well. This way you know what tab is clicked, since it gets focus before it gets removed.

Code:
Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        'For Each p As TabPage In TabControl1.TabPages
        '    AddHandler p.MouseDoubleClick, AddressOf TabPage_Close
        'Next

        AddHandler TabControl1.MouseDoubleClick, AddressOf TabPage_Close

    End Sub

    Private Sub TabPage_Close(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        TabControl1.TabPages.Remove(CType(sender, TabControl).SelectedTab)
    End Sub

~ DP
 
Back
Top