C# 如何在 Windows 窗体中创建没有选项卡标题的选项卡控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10316567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a Tab Control with no Tab Header in Windows form?
提问by Running Rabbit
I have created a Windows form using a Tab Control, but it has a header with it. I want to hide it. I am not able to do it using any properties of the Tab Control. Is there any property defined for hiding the tab header for the Tab Control without going through the code?
我使用选项卡控件创建了一个 Windows 窗体,但它有一个标题。我想隐藏它。我无法使用选项卡控件的任何属性来执行此操作。是否定义了任何属性来隐藏选项卡控件的选项卡标题而无需通过代码?
回答by Lukasz M
You want the tab panels without the feature allowing a user to switch between them, so I suppose you want to create few separate sets of controls to be shown to the user one at a time. You can achieve this in several ways (you can choose one of them if you find it appropriate in your case):
您希望选项卡面板没有允许用户在它们之间切换的功能,因此我想您希望创建几组单独的控件以一次向用户显示一个。您可以通过多种方式实现这一点(如果您认为适合您的情况,您可以选择其中一种):
- Use several
Panelcontrols instead of several tabs in theTabControl, however, it would be hard to work in the designer, because all the controls would be visible - Use different
Forms instead of tabs to keep the layout parts separated. It can be ok, but you may not want to use multipleForms, so it depends on a specific case.
- 使用多个
Panel控件,而不是在几个标签TabControl,然而,这将是很难在设计师的工作,因为所有的控制是可见 - 使用不同的
Forms 而不是制表符来保持布局部分分开。可以,但是您可能不想使用多个Forms,因此这取决于特定情况。
and finally, the suggested solution:
最后,建议的解决方案:
- Encapsulate each set of controls in a
UserControl. This allows you to keep each layout separately, so you can easily design each of them without the other controls getting in the way ;). The the code handling each of the layouts would also be separated. Then just drag those controls in the Form and use set their visibilities appropriately to show the one you want.
- 将每组控件封装在一个
UserControl. 这允许您单独保留每个布局,因此您可以轻松地设计每个布局,而不会受到其他控件的影响;)。处理每个布局的代码也将分开。然后只需在表单中拖动这些控件并使用适当的设置它们的可见性来显示您想要的。
If none of those suggestions work for you, let me know, so I can look for other possible solutions.
如果这些建议都不适合您,请告诉我,以便我寻找其他可能的解决方案。
回答by Romil Kumar Jain
Use following code to hide the tabs or set these properties in design.
使用以下代码隐藏选项卡或在设计中设置这些属性。
tabControl.Appearance = TabAppearance.FlatButtons;
tabControl.ItemSize = new Size(0, 1);
tabControl.SizeMode = TabSizeMode.Fixed;
回答by Phil
It's more easy as you think, you just drag the panel's window upper, so will be outside of the form.
它比您想象的更容易,您只需将面板的窗口拖到上方,这样就会在表单之外。
回答by Massimo
Another way to achieve the same (or similar) is: You can remove tabs from TabControl.TabPages collection and then add the tab you want to show.
实现相同(或类似)的另一种方法是:您可以从 TabControl.TabPages 集合中删除选项卡,然后添加要显示的选项卡。
During the Form initialization I remove tabs (so into the designer I can easily manage them) and in some control event (as button click) I show the tab the user has to see.
在表单初始化期间,我删除了选项卡(因此我可以轻松地在设计器中管理它们)并在某些控件事件(如按钮单击)中显示用户必须查看的选项卡。
Something like that:
类似的东西:
// During form load:
ctrTab.TabPages.Clear();
// ......
// During button click or some other event:
if(rbSend.Checked)
ctrTab.TabPages.Add(pgSend);
else
ctrTab.TabPages.Add(pgReceive);
In this way the user can still see the header tab but just as title of controls group, he can't change/switch the current active tab.
通过这种方式,用户仍然可以看到标题选项卡,但就像控件组的标题一样,他无法更改/切换当前活动选项卡。
回答by monikapatelIT
Use DrawMode: OwnerDrawFixed will hide TabPage header text DrawMode : OwnerDrawFixed
使用 DrawMode: OwnerDrawFixed 将隐藏 TabPage 标题文本DrawMode : OwnerDrawFixed

