2011-02-16 50 views
0

我編寫了代碼以顯示右鍵單擊我的tabpages上下文菜單。當用戶從上下文菜單中點擊「刪除標籤」時,我將如何去實際刪除標籤頁?我已經得到了這個。 (unloadProfile是我的上下文菜單項)。我不確定如何獲取上下文菜單正在關聯的tab頁以將其刪除。任何幫助表示讚賞。如何從上下文菜單中刪除tabpage

// My Context Menu 
private void tabControl_MouseClick(object sender, MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Right) 
     { 
      // iterate through all the tab pages 
      for (int i = 0; i < tabControl.TabCount; i++) 
      { 
       // get their rectangle area and check if it contains the mouse cursor 
       Rectangle r = tabControl.GetTabRect(i); 
       if (r.Contains(e.Location)) 
       { 
        // show the context menu here 
        this.contextMenuStrip1.Show(this.tabControl, e.Location); 
       } 
      } 
     } 
    } 

// Context menu click event 
private void unloadProfile_Click(object sender, EventArgs e) 
    { 
     // iterate through all the tab pages 
     for (int i = 0; i < tabControl.TabCount; i++) 
     { 

     } 
    } 

回答

3

我不認爲這是做到這一點的正確方法,但它的工作原理。

在tabControl1_MouseClick(對象發件人,MouseEventArgs e)事件中,將menustrip的Tag屬性設置爲選中的TabPage。

// show the context menu here 
this.contextMenuStrip1.Tag = this.tabControl1.TabPages[i]; 
this.contextMenuStrip1.Show(this.tabControl1, e.Location); 

而在removeTabToolStripMenuItem_Click(對象發件人,EventArgs的)事件中使用Tag屬性

this.tabControl1.TabPages.Remove(this.contextMenuStrip1.Tag as TabPage); 

空校驗將是很好的去除標籤頁:)希望它幫助。

+0

很好用。謝謝。 – 2011-02-16 05:21:28

相關問題