2015-05-06 56 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; 
    } 
} 

}