[RESOLVED] Calculate a String's size on screen
I have a custom button that needs to use graphics.drawstring for its text property. Does anybody know how to get what the string’s length will be when it's on the screen. It'd be great if you where able to view the code from the class Buttonbase to see how it's done.
At the moment I just have a hidden label that gets added to the same parent container as the button and then read the labels width property
Any suggestions?
Ger
Re: Calculate a String's size on screen
You need to use the graphics measureString method.
vb Code:
Private Sub Button1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Button1.Paint
Dim strTest As String = "Test String"
Dim g As Graphics = e.Graphics
Dim sz As SizeF
'get the size of the string
sz = g.MeasureString(strTest, Me.Font)
'draw it where we want.
g.DrawString(strTest, Me.Font, Brushes.Black, 5, 5)
End Sub
Re: Calculate a String's size on screen
Quote:
Originally Posted by Tinbeard
You need to use the graphics measureString method.
vb Code:
Private Sub Button1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Button1.Paint
Dim strTest As String = "Test String"
Dim g As Graphics = e.Graphics
Dim sz As SizeF
'get the size of the string
sz = g.MeasureString(strTest, Me.Font)
'draw it where we want.
g.DrawString(strTest, Me.Font, Brushes.Black, 5, 5)
End Sub
Perfect,
Thanks for your help.
I don't know how i missed that function in graphics.
Ger