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

只谈情不闲聊 提交于 2019-12-23 16:41:59

问题


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?


回答1:


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.




回答2:


You should be doing your stuff on

TabIndexChanged

If you want to load the contents on your 3rd tab, for example, handle the TabIndexChanged on your tab control, do a switch for each tabPage.Index and then do whatever is needed when user clicks on that tab.




回答3:


    private void tabPage1_Layout(object sender, LayoutEventArgs e)
    {
       //do something
    }


来源:https://stackoverflow.com/questions/25478922/how-to-trigger-event-when-clicking-on-a-selected-tab-page-header-of-a-tab-contro

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!