冻结 C#.net 选项卡控件中的第一个选项卡

Freeze first tab in C#.net tab control

有没有办法冻结 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);
                }
            }

我找到了冻结第一个选项卡的方法。

上述解决方案的问题是,我试图在 paint 方法中操作标签页列表。导致反复调用paint方法产生异常

所以我试图操纵从 "this.GetTabTextRect(index)" 返回的制表符位置。它运作良好。但是我也不得不写一些代码来调整选项卡选择逻辑。

受保护的矩形 GetTabTextRect(int index) {

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

}