有没有办法冻结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 :(得分:0)
我找到了冻结第一个标签的方法。
上述解决方案的问题是,我试图操纵paint方法中的标签页列表。它导致一遍又一遍地调用paint方法并生成异常。
所以我试图操纵从&#34; this.GetTabTextRect(index)&#34;返回的标签位置。它工作正常。但我必须编写一些代码来调整选项卡选择逻辑。
protected Rectangle 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;
}
}
}