Hack Forums
[.NET] Align a form's text/title. - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Align a form's text/title. (/showthread.php?tid=5168384)



Align a form's text/title. - Exodus. - Feb 12, 2016

Not sure how to do this. I just want to align my Form title to the middle. That's all.


* - AnonCensored - Feb 12, 2016

Add space's before the title name ?


* - iLogic™ - Feb 13, 2016

(Feb 12, 2016 10:19 PM)Exodus. Wrote:  Not sure how to do this. I just want to align my Form title to the middle. That's all.
In windows 8/8.1 form titles are automatically aligned to the middle:
[Image: wwMbRBS.png]
What OS are you using?


* - Specs - Feb 13, 2016

I had this issue before, i dont think there is a way to do it through simple properties. Using google i found this for you.

Code
Private Sub CenterMe()
    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

    Me.Refresh()
End Sub

Just call "CenterMe()" On Form_Load event & you should be good. I think a more simpler way would to just add a couple spaces before like someone mentioned, but i guess this works correctly.


* - Odus - Feb 13, 2016

There are no direct methods to this, you would have to draw your own form.

(Feb 13, 2016 04:27 AM)Specs Wrote:  I had this issue before, i dont think there is a way to do it through simple properties. Using google i found this for you.

Code
Private Sub CenterMe()
    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

    Me.Refresh()
End Sub

Just call "CenterMe()" On Form_Load event & you should be good. I think a more simpler way would to just add a couple spaces before like someone mentioned, but i guess this works correctly.

This would need to be called on form resize. It's not very efficient either, cleaned it up:

Code
Private Sub CenterMe()
    Using g As Graphics = Me.CreateGraphics()
        Dim startPoint As Double = (Me.Width / 2) - (g.MeasureString(Me.Text.Trim(), Me.Font).Width / 2)
        Dim spaceWidth As Double = g.MeasureString(" ", Me.Font).Width

        Me.Text = New String(" "c, CInt(startPoint / spaceWidth)) & Me.Text.Trim()
    End Using

    Me.Refresh()
End Sub



* - Earn - Feb 13, 2016

This is what I do with GDI+ themes.

Code
Dim G = e.Graphics
Dim F As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
G.DrawString(Text, Font, New Rectangle(0, 0, Width - 1, 40), F)

Replace 40 for the height of the header. Sorry if there are any mistakes, types that all out on my phone.


* - Ninja - Feb 13, 2016

(Feb 13, 2016 05:34 AM)Earn Wrote:  This is what I do with GDI+ themes.

Code
Dim G = e.Graphics
Dim F As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
G.DrawString(Text, Font, New Rectangle(0, 0, Width - 1, 40), F)

Replace 40 for the height of the header. Sorry if there are any mistakes, types that all out on my phone.

But you dont declare the type of G nor do you dispose of G afterwards...


* - Earn - Feb 13, 2016

(Feb 13, 2016 05:43 AM)Tyranitar Wrote:  But you dont declare the type of G nor do you dispose of G afterwards...

I just done it quickly as I am on my phone. I only put the "Dim G = e.Graphics" there so the OP wasn't confused by the "G.DrawString()".


* - Ninja - Feb 13, 2016

(Feb 13, 2016 05:47 AM)Earn Wrote:  I just done it quickly as I am on my phone. I only put the "Dim G = e.Graphics" there so the OP wasn't confused by the "G.DrawString()".

Code
Using G as Graphics = e.Graphics
    Dim F As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
    G.DrawString(Text, Font, New Rectangle(0, 0, Width - 1, 40), F)
End Using
Cleaned it a bit up for you.


* - Earn - Feb 13, 2016

(Feb 13, 2016 05:49 AM)Tyranitar Wrote:  
Code
Using G as Graphics = e.Graphics
    Dim F As New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}
    G.DrawString(Text, Font, New Rectangle(0, 0, Width - 1, 40), F)
End Using
Cleaned it a bit up for you.

Disposing isn't needed, as the graphics object will get disposed when you exit the method.


* - Ninja - Feb 13, 2016

(Feb 13, 2016 05:54 AM)Earn Wrote:  Disposing isn't needed, as the graphics object will get disposed when you exit the method.

Oh I didn't know that. Still wouldn't hurt for good practice though.