Click here to Skip to main content
Sign Up to vote bad
good
See more: C#Windows.NETForms
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:37

Comments
BillWoodruff - 26 Sep '11 - 10:50
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:17
thx BillWoodruff, I realized you are right but I will try some more

4 solutions

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
 
Do  
  tmp += " "
  tmpWidth += widthOfASpace
Loop While (tmpWidth + widthOfASpace) < startingPoint
        
Me.Text = tmp & Me.Text.Trim & tmp
 
 
This worked well for me.
 
- Pete
  Permalink  
Comments
Marcus Kramer - 27 Jul '12 - 9:24
This is a very old question that already had an answer. Please don't answer these old ones.
pdoxtader - 27 Jul '12 - 9:30
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:30
don't keep information for yourself and try to help others.
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));
  Permalink  
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();
        }
  Permalink  
There is no direct way and try this
 
http://www.codeproject.com/KB/dialog/FormEx.aspx]
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 146
1 Richard MacCutchan 145
2 Sergey Alexandrovich Kryukov 134
3 Tadit Dash 134
4 Santhosh G_ 120
0 Sergey Alexandrovich Kryukov 10,348
1 OriginalGriff 7,965
2 CPallini 4,241
3 Rohan Leuva 3,522
4 Maciej Los 3,184


Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 15 Aug 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid