2015-05-06 52 views
-1

有沒有辦法凍結C#.net標籤頁控件的第一個標籤頁?凍結C#.net標籤控件中的第一個標籤頁

我有一個可以有許多選項卡的選項卡控制器。如果用戶正在滾動它們,第一個標籤應該保持在第一個位置,剩下的標籤應該移動。

我試圖使用刪除並在paint方法中插入選項卡。但似乎是我試圖刪除並添加第一頁時,有時會獲得索引問題。

  // For the first time home tab comes as the first tab item 
      if (this.homeTab == null) 
      { 
       this.homeTab = this.TabPages[0]; 
      } 

      // get initial first display index to a temp variable 
      int tempFirstIndex = -1; 
      for (int index = 0; index < this.TabCount; index++) 
      { 
       Rectangle currentTabBounds = this.GetTabTextRect(index); 

       if (currentTabBounds != Rectangle.Empty && tempFirstIndex < 0 && currentTabBounds.X >= 0) 
       { 
        tempFirstIndex = index; 
        break; 

       } 
      } 

      int homeTabIndex = this.TabPages.IndexOf(this.homeTab); 
      Rectangle homeTabBounds = this.GetTabTextRect(homeTabIndex); 

      if (homeTabIndex > tempFirstIndex) 
      { 
       this.TabPages.Remove(this.homeTab); 
       this.TabPages.Insert(tempFirstIndex, this.homeTab); 
      } 
      else 
      { 
       // find the first visible position 
       // it can not be simply the tempFirstIndex, because in this scenario tab is removed from begining 
       // tabs are not same in width 
       while (homeTabBounds != Rectangle.Empty && homeTabBounds.X < 0) 
       { 
        homeTabIndex++; 
        this.TabPages.Remove(this.homeTab); 
        this.TabPages.Insert(homeTabIndex, this.homeTab); 
        homeTabBounds = this.GetTabTextRect(homeTabIndex); 
       } 
      } 
+0

我認爲你需要更好地解釋你的問題。無論您打開哪個頁面,第一個標籤頁將始終保持第一個頁面。 – DotNetHitMan

+0

如果您的顯示區域中顯示的選項卡多於其顯示區域,則會隱藏第一個選項卡。然後,您應該使用滾動控件導航到第一頁。希望我已經解釋了這個問題。基本上我需要凍結第一個標籤。 – user3720148

回答

0

我已經想出了凍結第一個選項卡的方法。

上述解決方案的問題是,我試圖操縱paint方法內的標籤頁列表。它會導致一遍又一遍地調用paint方法並生成異常。

所以我試圖操縱從「this.GetTabTextRect(index)」返回的標籤位置。它運行良好。但是我必須編寫一些代碼來調整選項卡選擇邏輯。

保護的矩形GetTabTextRect(INT指數) {

// gets the original tab position 
Rectangle tabBounds = this.GetTabRect(index); 

// first displayed index will be calculated in the paint method 
// if it is equal or less than to zero means, the number of tabs are not exceeded the display limit. So no need tab manipulating 
if (this.firstDisplayedIndex > 0) 
{ 
    // if it is not first tab we adjust the position 
    if (index > 0) 
    { 
     if (index > this.firstDisplayedIndex && index <= this.lastDisplayedIndex) 
     { 
      Rectangle prevBounds = this.CurrentTabControl.GetTabRect(this.firstDisplayedIndex); 

      // home tab (first tab) bounds will be stored in a global variable when the tab control initializes 
      tabBounds.X = tabBounds.X - prevBounds.Width + this.homeTabBounds.Width; 
     } 
     else if (index == this.firstDisplayedIndex) // we need to free this slot for the first tab (index = 0) 
     { 
      tabBounds.X -= 1000; 
     } 
    } 
    else 
    { 
     // first tab position is fixed 
     tabBounds.X = 0; 
    } 
} 

}