以编程方式更改 Tab 键顺序
如何以编程方式对 TabControl
中的选项卡重新排序?我需要根据某些条件对选项卡进行排序。
如果可以通过设计器进行重新排序,我想我们也必须能够在运行时通过代码来完成。
How do I programmatically reorder the tabs in a TabControl
? I need to sort the tabs depending on some conditions.
If it's possible to do the reordering through the designer, i guess we must be able to do it through code at runtime too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
this.TabControl1.TabPages.Remove(this.TabPage2)
this.TabControl1.TabPages.Insert(0, this.TabPage2)
请注意,如果您无法删除标签页,它仍会显示在原来的位置。换句话说,您将在同一选项卡页上拥有两个选项卡。
this.TabControl1.TabPages.Remove(this.TabPage2)
this.TabControl1.TabPages.Insert(0, this.TabPage2)
Note that if you fail to remove the tab page, it will still show at its old location. In other words, you will have two tabs of the same tab page.
您必须重新定义标签页集合,才能更改标签页的索引。
You have to redefine your tab page collection, in order to change the index of your tab pages.
下面的代码行也可以做到这一点,这种解决方案也适用于其他没有直接排序方式的数据:
- 转换为列表
- 对列表进行排序
- 把它放回去
The following lines of code can also do the trick, this kind of solution also works for other kind of data that has no direct way of sorting:
- convert to a list
- sort the list
- put it back
失去的是对的。下面是一个快速示例代码。
我有一个带有 2 个选项卡的选项卡控件(tabpage1、tabpag2)
然后我声明两个选项卡页并将选项卡控件中的现有选项卡存储在其中。
然后单击按钮,
如果您想更改顺序,我使用“现在”删除了选项卡,那么您必须按该顺序将其添加到选项卡
注意:这是未经测试的快速代码。
thelost is right. Below is a quick sample code.
I have a tab control with 2 tabs (tabpage1, tabpag2)
Then I declare two tabpages and store the existing tabs in the tabcontrol in it.
Then on a button click I removed the tabs using
Now if you want to change the order then you will have top add it to the tab in that order
Note: This is untested quick code.
进入 Designer.cs 文件
,您会发现
添加顺序是选项卡控件中选项卡页的顺序。按照您的意愿更改顺序。 TabControl.Controls 的删除和添加功能将帮助您,正如 Shoban 所回答的那样。
Go inside Designer.cs file
There you will find
The adding order is your tabpages' order in the tabcontrol. Change the order how you wish. Remove and Add functions of TabControl.Controls will help you as Shoban answered.
在 InitilizaComponent() 之后尝试这个。此代码将让您可以自由地在 .cs 文件中以编程方式更改它。
try this after Initilizacomponent(). this code will give you freedom to change it programmatically in .cs file.
有时我有带有多个选项卡页面的选项卡控件。在运行时,选项卡页面变得不可见(通过删除它们)并稍后再次添加。
此后,选项卡页面的顺序可能会错误。我使用此代码再次对它们重新排序:
Sometimes I have tabControls with several tabPages. At runtime tabPages are made invisible (by removing them) an added later again.
After this the tabPages may be in wrong order. I use this code to redorder them again: