Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger event when clicking on a selected tab page header of a tab control

Tags:

c#

tabs

winforms

I am doing a Winform application in C# and I have some tab pages, say tabPage1, tabPage2 and tabPage3, in a tab control, and tabPage1 is selected.

I want to trigger event when any tab page header is clicked, but I could do it only for page change (by using SelectedIndexChanged) but not click on a selected tab page header.

I tried with Selecting and Selected events but both of them didn't not work. I searched on MSDN but didn't find any Click event defined on a page header. So how should I achieve this?

One further question, is it possible, and how, to detect DoubleClick on a selected tab page?

like image 788
nevets Avatar asked Oct 27 '25 08:10

nevets


1 Answers

Just use the tabcontrol's MouseDoubleClick event. You'll have to iterate the tabs to find out what specific tab was clicked:

    private void tabControl1_MouseDoubleClick(object sender, MouseEventArgs e) {
        for (int ix = 0; ix < tabControl1.TabCount; ++ix) {
            if (tabControl1.GetTabRect(ix).Contains(e.Location)) {
                // Found it, do something
                //...
                break;
            }
        }
    }

Do keep in mind that this is completely undiscoverable to the user, he'll never think to double-click the tab. You'll have to write a manual.

like image 187
Hans Passant Avatar answered Oct 28 '25 21:10

Hans Passant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!