Click here to Skip to main content
Rate this: bad
good
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
Comments
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
good
Please Sign up or sign in to vote.

Solution 1

There is no direct way and try this
 
http://www.codeproject.com/KB/dialog/FormEx.aspx]
  Permalink  
Rate this: bad
good
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
 
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 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
good
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();
        }
  Permalink  
Rate this: bad
good
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));
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 468
1 Sergey Alexandrovich Kryukov 305
2 PhilLenoir 115
3 RajeeshMenoth 110
4 ChauhanAjay 92
0 Sergey Alexandrovich Kryukov 6,557
1 OriginalGriff 5,966
2 CPallini 2,473
3 Richard MacCutchan 1,687
4 Abhinav S 1,540


Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 15 Aug 2012
Copyright © CodeProject, 1999-2014
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