I'm using GDI to write out a string on a custom control that I am creating. How can I get the width of that string so I can make it centered in the control?
Thanks
Printable View
I'm using GDI to write out a string on a custom control that I am creating. How can I get the width of that string so I can make it centered in the control?
Thanks
There is a MeasureString function available in the Graphics Class that do so.
But you have to count manualy the cariage return in it.
Thanks
You can also use a stringformat which will let you set the alignment - and will wrap the text for you:
VB Code:
Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint ' stringformat lets you set the alignment: Dim sf As StringFormat = StringFormat.GenericDefault.Clone sf.Alignment = StringAlignment.Center ' horiz alignment sf.LineAlignment = StringAlignment.Center ' vert alignment ' look at overload list for drawstring to see one that uses stringformat: ' Overloads Public Sub DrawString(String, Font, Brush, RectangleF, StringFormat) ' make a rectangleF that is the size of the label Dim rec As New RectangleF(0, 0, Label1.Width, Label1.Height) ' draw string: e.Graphics.DrawString("test", Me.Font, Brushes.Black, rec, sf) End Sub
Good thing to know !! :)Quote:
Originally Posted by jo0ls