Click here to Skip to main content
13,355,538 members (71,493 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 2:37am
BillWoodruff 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 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 27-Jul-12 9:24am
This is a very old question that already had an answer. Please don't answer these old ones.
pdoxtader 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 15-Aug-12 5:30am
don't keep information for yourself and try to help others.
Ank_ush 4-Feb-16 2:09am
The below code I modified and it worked well for me, but now There is a new query, i.e. I have created MDI Parent and Mdi Child Forms. In Mdi Child forms the same code works, but it doesn't work in Mdi Parent form, Is there any solution???
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();
ajSterman 24-Oct-17 3:19am
changed to :
Double startingPoint = (this.Width / 2) - (g.MeasureString(this.Text.Trim(), this.Font).Width);
and worked for me, I don't have an explanation.
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
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180111.1 | Last Updated 15 Aug 2012
Copyright © CodeProject, 1999-2018
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