Re: How to word wrap tab title in tab control



Visual Styles only applies to XP and above. In this case you'll need to
ownerdraw the Tabs.

Here's a basic example for top or bottom aligned tabs with no Image, that
allows you to include the full text at designtime.
(assumes tabControl is named tabControl1).

\\\
private Hashtable multiLineTabs = new Hashtable();
private bool multiLine;
private void Form1_Load(object sender, System.EventArgs e)
{
foreach(TabPage tab in this.tabControl1.TabPages)
{
int newLineIndex = tab.Text.IndexOf(@"\n");
if (newLineIndex !=- 1)
{
if (!multiLine)
{
multiLine = true;
tabControl1.Padding = new Point(6, tabControl1.Font.Height);
}
multiLineTabs.Add(tab,tab.Text.Remove(0,newLineIndex + 2));
tab.Text = tab.Text.Substring(0,newLineIndex);
}
}
}

private void tabControl1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
TabControl tabControl = (TabControl)sender;
TabPage currentTab = tabControl.TabPages[e.Index];
Graphics g = e.Graphics;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;

string tabText = currentTab.Text;

RectangleF tabRect = (RectangleF)e.Bounds;
RectangleF textRect = tabRect;

if (e.Index == tabControl.SelectedIndex)
{
tabRect.Inflate(1,1);
}

g.Clip = new Region(tabRect);
g.Clear(Control.DefaultBackColor);
g.ResetClip();

if (multiLine)
{
if (multiLineTabs.Contains(currentTab))
{
tabText += "\n" + (string)multiLineTabs[currentTab];
}
}

g.DrawString(tabText,e.Font,SystemBrushes.ControlText,textRect,sf);

}
///

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html


.



Relevant Pages

  • Re: Sharing a component across multiple tabs
    ... how about making a tab control that is only as tall as the tabs themselves.. ... I have to use the TabControl - our users expect it as a result ... TabControl next to the data panel, but make it look as if it is the ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: changing backcolor of tabcontrol
    ... that the tabs of my tabControl won't be visible. ... If I don't set the user-bite in the described way the Color of my TabControl ... > question just override the paint event for the individual tab page. ...
    (microsoft.public.dotnet.framework.windowsforms.designtime)
  • Re: TabControl question.
    ... Tabs to go in it, ... I would create in designed a single TabControl containing ALL ... would initialize 3 arrays of TabPages. ... You must declare the array as ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Container for VisualStyles
    ... Hide the tabs by altering the TabControls DisplayRectangle ... that can be added to a TabControl is a TabPage. ... Microsoft Online Community Support ... where an initial response from the community or a Microsoft Support ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: An array of panels for a configuration window
    ... Keep the TabControl, but use a custom one in which you can hide the tabs. ... > right side of the window will display a panel that contains the related ... > configuration options. ...
    (microsoft.public.dotnet.languages.csharp)