Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Align a form's text/title.

Not sure how to do this. I just want to align my Form title to the middle. That's all.
Add space's before the title name ?
(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?
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.
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
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.
(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...
(Feb 13, 2016 05:43 AM)Ninja 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()".
(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.
(Feb 13, 2016 05:49 AM)Ninja 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.
(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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Java] by mattFeb 11, 2017 10:47 AM 5 278 Feb 11, 2017 10:47 AM
Last Post: matt
[Python] by -Matatan-Nov 25, 2016 05:15 PM 9 250 Nov 25, 2016 05:15 PM
Last Post: -Matatan-
[.NET] by KullsMay 18, 2016 08:11 AM 13 347 May 18, 2016 08:11 AM
Last Post: halmat99