Custom tab control in windows forms

18,369

Why this happens? I'm not sure... I believe the Framework just uses the underlying Windows API, so if it is different between Windows versions, a .Net application might look/behave slightly differently. I wonder if there is a culture/string behavior setting somewhere that might take care of this...

Anyways, here is my sloppy-but-effective solution to your problem: Just insert a line break into the text of your tab (I don't know if you can do this from the designer). You can do this from your own C# code or from the designer's code-behind C#. You also must make the tab size bigger in order to accommodate for the additional text line.

        // Set the size of the tabs.  I multiply the default height by 2 for 2 lines
        tabControl1.ItemSize = new System.Drawing.Size(77, 18 * 2);

        // Force a line break within the string
        tabControl1.Controls[0].Text = "Hello\r\nWorld"; 
Share:
18,369
Max Kilovatiy
Author by

Max Kilovatiy

Passionate Microsoft-certified software engineer specializing in information system integration for processing financial data. Over 5 years hands-on experience in IT, development of client-server applications using TypeScript, Angular, Node.js, REST services. Development and support of business software solutions for commercial customers and public authorities Technical skills: Languages: TypeScript, JavaScript, C#, T-SQL, PL/SQL, Java, Python Technologies, frameworks, products: Angular, NodeJS, NestJs, Express, WebApi, Jest & Spectator

Updated on June 04, 2022

Comments

  • Max Kilovatiy
    Max Kilovatiy almost 2 years

    I have faced with strange behavior of tab control in Windows XP and in Windows 7. In XP text that lay on tab is wrapped and in 7 is no. Whats the matter with tab?enter image description here

    PS. non English letters are wrapped

    UPDATE: this is a custom tab control

    Here is the code:

    protected override void OnPaint(PaintEventArgs e)
        {
            DrawControl(e.Graphics);
        }
    
    internal void DrawControl(Graphics g)
        {
            if (!Visible)
                return;
    
            Brush br = new SolidBrush(clr_cl_area);
            Brush brTab = new SolidBrush(clr_client);
    
            Rectangle TabControlArea = ClientRectangle;
            Rectangle TabArea = DisplayRectangle;
    
            g.FillRectangle(br, TabControlArea);
            g.FillRectangle(brTab, TabArea);
    
            br.Dispose();
            brTab.Dispose();
    
            for (int i = 0; i < TabCount; i++)
                DrawTab(g, TabPages[i], i, false);
    
            if (_mouseTabIndex != null && _mouseTabIndex != _mouseTabIndexSave && _mouseTabIndex != SelectedIndex)
                DrawTab(g, TabPages[(int)_mouseTabIndex], (int)_mouseTabIndex, true);
    
            _mouseTabIndexSave = _mouseTabIndex;
    
        }
    
        internal void DrawTab(Graphics g, TabPage tabPage, int nIndex, bool mouseOverTab)
        {
    
            var recBounds = GetTabRect(nIndex);
    
    
            SetBounds(ref recBounds);
            var pt = SetPointsForTabFill(recBounds);
    
    
            DrawTabBounds(g, recBounds);
    
    
            FillTabl(g, recBounds, pt, false);
    
    
            DrawTabSeparators(g, recBounds, nIndex, 0 /*y-bottom*/);
    
    
            if (SelectedIndex == nIndex) 
            {    
                DrawTabGradient(g, recBounds, pt, nIndex,0/*width*/,1/*height*/);  
                DrawTabSeparators(g, recBounds, nIndex, 1 /*y-bottom*/); 
            }
    
            if (mouseOverTab)
                DrawTabGradient(g, recBounds, pt, nIndex, -2/*width*/, 0/*height*/);
    
            DrawText(g, recBounds, tabPage.Text);
    
        }
    
        private void DrawText(Graphics g, Rectangle recBounds, string text)
        {
            var strFormat = new StringFormat();
            strFormat.Alignment = strFormat.LineAlignment = StringAlignment.Center;
    
            g.TextRenderingHint =
                System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
    
            //var fnt = new Font(MsFonts.familyPTSans, 8F, FontStyle.Regular,  GraphicsUnit.Point, (byte)204);
            var fnt = new Font("Arial", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(204)));
    
            RectangleF tabTextArea = recBounds;
            var br = new SolidBrush(clr_txt);
            g.DrawString(text, fnt, br, tabTextArea, FormatString());
    
            br.Dispose();
            strFormat.Dispose();
            fnt.Dispose();
        }