Winforms TabControl alignment problems

南楼画角 提交于 2019-12-05 00:42:14

问题


When I set TabControl alignment to Left or Right it leaves this huge space between tab buttons and tab page area. How to get rid of this useless space?

TabControl.Appearance is set to Buttons because if it is set to Normal the text on buttons disappear.

UPDATE:
When i set TabControl.Alignment to Bottom and TabControl.Appearance to Normal buttons look inverted (orange line should be below)

When i set TabControl.Alignment to Bottom and TabControl.Appearance to Buttons, There is no area on TabPage to place controls


回答1:


This is a well-known problem with the XP visual style implementation for the native tab control, only tabs aligned to the top render properly. This bug hasn't been addressed until Windows 7. The workaround is to selectively turn off the style. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form and change the Alignment property to your liking. It isn't going to look prettier than that.

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class FixedTabControl : TabControl {

    protected override void OnHandleCreated(EventArgs e) {
        SetWindowTheme(this.Handle, "", "");
        base.OnHandleCreated(e);
    }

    [DllImportAttribute("uxtheme.dll")]
    private static extern int SetWindowTheme(IntPtr hWnd, string appname, string idlist);
}



回答2:


Microsoft documentation Remarks related to this issue

When the Alignment property is set to Left or Right, the Multiline property is automatically set to true.

When you set the Appearance property to FlatButtons, it only appears as such when the Alignment property is set to Top. Otherwise, the Appearance property displays as if set to the Buttons value.

When you set the Appearance property to Buttons, you must also set the Alignment property to Top so that the buttons display correctly.

Note

When you set the Appearance property to Buttons, you must also set the Alignment property to Top so that the tab page contents display correctly. Additionally, when visual styles are enabled, and the Alignment property is set to a value other than Top, the tab contents may not render correctly.



来源:https://stackoverflow.com/questions/4928076/winforms-tabcontrol-alignment-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!