473,322 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How to word wrap tab title in tab control

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
3 13976
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: yawnmoth | last post by:
word wrapping normally treats some spaces as line feeds, if there hasn't been a line feed for quite a while. so while a string with eighty consecutive a's might not word wrap, a space placed...
8
by: Darryl Kerkeslager | last post by:
I hope that although this is 25% Access and 75% Word, that someone will know ... The whole problem here arises because 1) Microsoft acknowledges an 'issue' wherein TextInput type FormFields are...
10
by: Jeff B. | last post by:
Has anyone come across a decent algorithm for implementing word wrap features in .net printing? I have a small component that uses basic printing techniques (i.e. e.Graphics.DrawString in a...
10
by: Jonathan Smith | last post by:
I have a VB app that has a richtextbox with the word wrap set to true. I need to be able to output the text to a text file, but it just puts the text in the file as one long line. How do i...
0
by: morph2007 | last post by:
Hi, I have my resume in good old HTML with Tables and CSS. I have two stylesheets, one to format for the web and the other for when I load the resume up into word - all works fine. Now - I...
10
by: Lorenzo Thurman | last post by:
I have a table cell that I want to wrap text inside of. I've tried both hard and soft wrap, but Firefox refuses to obey. IE 6&7 handle the wrap just fine. Does anyone know how I can fix this?
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
2
by: Sasie7679 | last post by:
Dear All, We currently use word in our application. We hide the title bar of word and display it in a ActiveX control (just the textual area). We use the below API for hiding the title bar. ...
8
by: Dave | last post by:
Hi all, I have a problem with DOM. If I have the xy coordinates in a web page, how can I get (possibly using Javascript) the corresponding word placed into the web page at the given...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.