Closing TabControl pages with middle click

Răspuns Closing TabControl pages with middle click

  • 2 mai 2012 06:23
     
      Are cod

    How can a tab pages in a TabControl be closed with the middle click, like they are in internet explorer? I found some similar questions here, but they don't quite answer this. One article that I found said to use this code:

    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());
                }

    but I found that this only works when I middle click within the page, as opposed to on the tab part.

    Thanks!


    -Winston

Toate mesajele

  • 2 mai 2012 07:45
     
     Răspuns Are cod

    private void tabControl1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button = MouseButtons.Middle)
        {
            TabPage tab = tabControl1.TabPages.Cast<TabPage>()                   
               .Where((t,i)=>   
                tabControl1.GetTabRect(i)
                .Contains(e.Location)).First();
            if (tab != null) tabControl1.TabPages.Remove(tab);
        }
    }



    • Editat de cicatrixx 2 mai 2012 07:50 forgot middleclick
    • Propus ca răspuns de Centigradz 2 mai 2012 09:20
    • Marcat ca răspuns de Zemus 3 mai 2012 02:27
    •  
  • 2 mai 2012 12:06
     
     

    Hi, 

    Seems there is no issues with the code you are using. Did you see any exceptions?


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".