|
-
Mar 14th, 2007, 03:19 AM
#1
Thread Starter
Junior Member
[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
-
Mar 14th, 2007, 03:59 AM
#2
Hyperactive Member
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
Last edited by Tinbeard; Mar 14th, 2007 at 04:07 AM.
If my post helps , please feel free to rate it 
-
Mar 14th, 2007, 05:09 AM
#3
Thread Starter
Junior Member
Re: Calculate a String's size on screen
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|