How to word wrap tab title in tab control

G

Guest

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?
 
M

Mick Doherty

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").
 
G

Guest

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
 
M

Mick Doherty

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);

}
///
 
Joined
Feb 27, 2008
Messages
1
Reaction score
0
How the wrapping of the tab text length be made for tab control with visual style

Hi,

How can the text of the tab pages be wrapped for the tab control with visual style property set to true.The Link in your reply does not work.

Please send an example for tab control with visual style property set to true.

Regards
Vkempanna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top