By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

How to word wrap tab title in tab control

P: n/a
Hi,

as a newbie to Visual Studio C# Forms development, I'd like some advice on
the following.

I have a form with a tab control. I have some tab titles that are three
words e.g "house & garden" This makes the tab quite long compared to other
tabs and means that not all tabs can be displayed on the screen.

I want these to be displayed on the tab title as
"house &
garden"

Any ideas how I can put this word wrap into the tab title?

Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Not supported, but not impossible.

You'll need to set the text at runtime rather than in the property browser.

With VisualStyles enabled you can get the string to wrap so long as it does
not have the ampersand in the string.
i.e. "house and\ngarden" will wrap but "house &\ngarden" will not (I have no
idea why). You then need to adjust the TabControls padding to allow for the
wrap.

Without VisualStyles the string will not wrap at all, so you'll have to
OwnerDraw to achieve the result.
You'll find several examples on my site:
http://dotnetrix.co.uk/tabcontrols.html

To OwnerDraw you'll need to set the text to "house &" in order to get the
correct width and then set the tabcontrols Padding to adjust the tab Height.
i.e. at Form_Load():

tabControl1.Padding = new Point(6, tabControl1.Font.Height + 3);

Using this method you will not be drawing the same text that is set in the
tabpages text property and so this makes things more complex.

The ownerDraw examples on my site do not include drawing Visual Styled tabs,
but if you need the tabcontrol both with and without them, then you might
consider using TabControlEX (available from my controls page
http://dotnetrix.co.uk/controls.html). You will still have to set Padding,
since the control is just a modified version of TabControl, but setting the
text at form_load will result in the wrapping.
TabControlEX also supports mnemonics so you'll need to double up on the
ampersand ("house &&\ngarden").

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
Nov 17 '05 #2

P: n/a
Hi Mike,

Ok, I've looked into VisualStyles and have added the line
Application.EnableVisualStyles() to my form. [What does
this do?]
To set the text I've added the following line to the Form1_Load method
tabControl.TabPages[1].Text = "House and \n\r Garden"
I've then included your padding line below.

This just adds a couple of lines representing the control characters into
the tab title, but doesn't wrap the text.

Is Enabling Visual Styles only applicable to XP applications I want to
deploy onto Windows 2000?

Any further suggestions as to what I've missed out, or got wrong?

Regards,

ColinC
"Mick Doherty" wrote:
Not supported, but not impossible.

You'll need to set the text at runtime rather than in the property browser.

With VisualStyles enabled you can get the string to wrap so long as it does
not have the ampersand in the string.
i.e. "house and\ngarden" will wrap but "house &\ngarden" will not (I have no
idea why). You then need to adjust the TabControls padding to allow for the
wrap.

Without VisualStyles the string will not wrap at all, so you'll have to
OwnerDraw to achieve the result.
You'll find several examples on my site:
http://dotnetrix.co.uk/tabcontrols.html

To OwnerDraw you'll need to set the text to "house &" in order to get the
correct width and then set the tabcontrols Padding to adjust the tab Height.
i.e. at Form_Load():

tabControl1.Padding = new Point(6, tabControl1.Font.Height + 3);

Using this method you will not be drawing the same text that is set in the
tabpages text property and so this makes things more complex.

The ownerDraw examples on my site do not include drawing Visual Styled tabs,
but if you need the tabcontrol both with and without them, then you might
consider using TabControlEX (available from my controls page
http://dotnetrix.co.uk/controls.html). You will still have to set Padding,
since the control is just a modified version of TabControl, but setting the
text at form_load will result in the wrapping.
TabControlEX also supports mnemonics so you'll need to double up on the
ampersand ("house &&\ngarden").

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

Nov 17 '05 #3

P: n/a
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,newLineInd ex + 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.ControlT ext,textRect,sf);

}
///

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.