Click here to Skip to main content
15,868,083 members
Please Sign up or sign in to vote.
1.00/5 (1 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
Comments
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

There is no direct way and try this

http://www.codeproject.com/KB/dialog/FormEx.aspx]
 
Share this answer
 
Just drop this code in your form's resize event:

VB
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
 
Share this answer
 
Comments
fjdiewornncalwe 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???
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.
C#
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();
        }
 
Share this answer
 
Comments
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.
C#
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));
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900