How to make WinPorms TabPage header title match title?

How can I make the WinPorms TabPage header title suitable for the title? Here is the problem.

enter image description here

+6
source share
3 answers

The built-in Windows tab control allows you to override the default minimum tab width. Unfortunately, this feature does not appear in the TabControl wrapper class. This is a fix though. Add a new class to your project and paste the code shown below. Compilation. Drop the new control on top of the toolbar onto the form.

using System; using System.Windows.Forms; class MyTabControl : TabControl { protected override void OnHandleCreated(EventArgs e) { base.OnHandleCreated(e); // Send TCM_SETMINTABWIDTH SendMessage(this.Handle, 0x1300 + 49, IntPtr.Zero, (IntPtr)10); } [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp); } 
+10
source

Thanks, Hans. I used your code without creating a class

 //InitializeComponent this.tabPresentations.HandleCreated += new System.EventHandler(TabControl_HandleCreated); void TabControl_HandleCreated(object sender, System.EventArgs e) { // Send TCM_SETMINTABWIDTH SendMessage((sender as TabControl).Handle, 0x1300 + 49, IntPtr.Zero, (IntPtr)4); } [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp); 
+3
source

You need to measure the fonts.

Try something like this:

 Dim tabPage As New TabPage Dim width As Integer = 0 Dim valueToMeasure As String = <Your title Here> Dim g As Graphics = tabPage.CreateGraphics() width = CType(g.MeasureString(valueToMeasure, tabPage.Font).Width, Integer) 

Probably add an extra bot to fill (width = width +10)

Edited by:

 <tab>.width = GetTabWidth(<Title>) Private Function GetTabWidth (Byval title as String) as Integer Dim widthValue as Integer = 10 'Padding (Optional) Dim tabPage as New tabPage Dim g as Graphics = tabPage.CreateGraphics() widthValue += Ctype(g.measureString(title, tabPage.Font).Width, Integer) Return widthValue End Function 
0
source

Source: https://habr.com/ru/post/893057/


All Articles