Click here to Skip to main content
11,795,848 members (77,134 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# Windows .NET Forms
I have windows application coded with c sharp using .net framework 3.5.
I need a dynamic way to center title bar text in all my application forms.

Please note that in my case I don't want to dock label at top and center it's text OR use a custom control.
All I need is to align the form caption at center.
thanks in advance
Posted 26-Sep-11 1:37am
BillWoodruff at 26-Sep-11 10:50am
I think you'll find it a very large waste of time to aim for this: the font and font-size of the titlebar text may vary with user system settings. And the interior area of the TitleBar available for text may vary depending on windows settings. I'm not saying it can't be done, but that it could be hard to do it right.
EgyptianRobot at 26-Sep-11 12:17pm
thx BillWoodruff, I realized you are right but I will try some more
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

There is no direct way and try this]
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Just drop this code in your form's resize event:

Dim g as Graphics = Me.CreateGraphics()
Dim startingPoint as Double = (Me.Width / 2) - (g.MeasureString(Me.Text.Trim, Me.Font).Width / 2)
Dim widthOfASpace As Double = g.MeasureString(" ", Me.Font).Width
Dim tmp As String = " "
Dim tmpWidth As Double = 0
  tmp += " "
  tmpWidth += widthOfASpace
Loop While (tmpWidth + widthOfASpace) < startingPoint
Me.Text = tmp & Me.Text.Trim & tmp

This worked well for me.

- Pete
Marcus Kramer at 27-Jul-12 9:24am
This is a very old question that already had an answer. Please don't answer these old ones.
pdoxtader at 27-Jul-12 9:30am
Why? I thought my much simpler solution might be of some help / interest to someone else who stumbles across this question, and doesn't want to use a custom form... I know I wouldn't want to.
EgyptianRobot at 15-Aug-12 5:30am
don't keep information for yourself and try to help others.
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

Just Create this method and Call in Form Load event of the form in which you want to center align the text of title bar.
private void Center_Text()
            Graphics g = this.CreateGraphics();
            Double startingPoint = (this.Width / 2) - (g.MeasureString(this.Text.Trim(), this.Font).Width / 2);
            Double widthOfASpace = g.MeasureString(" ", this.Font).Width;
            String tmp = " ";
            Double tmpWidth = 0;
            while ((tmpWidth + widthOfASpace) < startingPoint)
                tmp += " ";
                tmpWidth += widthOfASpace;
            this.Text = tmp + this.Text.Trim();
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Graphics g = CreateGraphics();
           double startingPoint = ((Width / 2) - (g.MeasureString(Text.Trim(), Font).Width / 2));
           double widthOfASpace = g.MeasureString(" ", Font).Width;
           string tmp = " ";
           double tmpWidth = 0;
           for ( ; ((tmpWidth + widthOfASpace) < startingPoint); )
               tmp += " ";
               tmpWidth = (tmpWidth + widthOfASpace);
           Text = (tmp  + (Text.Trim()+ tmp));

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 350
1 Sergey Alexandrovich Kryukov 326
2 KrunalRohit 239
3 Andy Lanng 236
4 OriginalGriff 230
0 OriginalGriff 1,852
1 Maciej Los 1,465
2 KrunalRohit 1,300
3 CPallini 840
4 Richard MacCutchan 828

Advertise | Privacy | Mobile
Web03 | 2.8.151002.1 | Last Updated 15 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100