Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

centering text on page when printing

Status
Not open for further replies.

aspro

Programmer
Jan 22, 2003
69
AU
I am trying to print a windows form i have created in VB .net. When printing the heading i use

ev.Graphics.DrawString(Label1.Text, headingFont, drawBrush, 390, 7)

This is puts it in the middle but not if the DPI changes. I am pretty much at the end of the line so any help would be greatly appreciated.

aspro
 
Use Graphics.MeasureString to get the length of the text you are going to draw, then use the value returned from
Code:
(Form.Width / 2) - (Text.Length / 2)
as your X parameter in DrawString to draw the text horizontally central.
 
thanks for ur help but i seem to be having a few problems with measurestring mainly the sizeF section of it. how can i convert it to an integer so that i can do the calculations?

aspro
 
Code:
Dim sfText As SizeF = g.MeasureString("My Text", New Font("Tahoma", 10))

Dim i As Int32 = Convert.ToInt32(sfText.Width)
 
How come I can't put the center x value into the drawline like this?

ev.Graphics.DrawString(Label1.Text, headingFont, drawBrush, middle, 7)

thanks
aspro
 
What is the variable
Code:
middle
?

If it is the centre of the form (i.e. the form width divided by 2) then using it as the X parameter of DrawString will result in the text being drawn slightly off centre.

If it's not the centre of the form, what is it ?
 
Sorry should have explained a little better.
But all is good I have fixed it all up and it works perfect.
Thanks a lot for ur help.

aspro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top