คำตอบ Closing TabControl pages with middle click

  • 2 พฤษภาคม 2555 6:23
     
      มีโค้ด

    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

ตอบทั้งหมด

  • 2 พฤษภาคม 2555 7:45
     
     คำตอบ มีโค้ด

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



    • แก้ไขโดย cicatrixx 2 พฤษภาคม 2555 7:50 forgot middleclick
    • เสนอเป็นคำตอบโดย Centigradz 2 พฤษภาคม 2555 9:20
    • ทำเครื่องหมายเป็นคำตอบโดย Zemus 3 พฤษภาคม 2555 2:27
    •  
  • 2 พฤษภาคม 2555 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".