Results 1 to 7 of 7

Thread: MeasureString problem [RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member wolfrose's Avatar
    Join Date
    Aug 2002
    Location
    Indiana
    Posts
    309

    MeasureString problem [RESOLVED]

    I'm making an invoice printing program and I would like to print the invoice number in a box and center it automatically.

    VB Code:
    1. Dim invSize As SizeF = e.Graphics.MeasureString(InvNumber, fFont)
    2.         Dim iX As Integer
    3.         iX = 560 + (200 - CInt(invSize.Width) / 2)

    The number is now right justified in the box, not in the center. What am I doing wrong?

    560 is the x coordinate of the box, and 200 is the width.
    Last edited by wolfrose; Aug 8th, 2003 at 04:48 PM.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Made its the math order try:
    VB Code:
    1. iX = 560 + (200 - (CInt(invSize.Width) / 2))

  3. #3

    Thread Starter
    Hyperactive Member wolfrose's Avatar
    Join Date
    Aug 2002
    Location
    Indiana
    Posts
    309
    VB does multiplication and division before addition and subtraction. I tried it just in case, but to no avail.

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    So where is it putting the box? Do you have some sample code we can use to test?

  5. #5

    Thread Starter
    Hyperactive Member wolfrose's Avatar
    Join Date
    Aug 2002
    Location
    Indiana
    Posts
    309
    VB Code:
    1. e.Graphics.DrawString("Invoice Number", bFont, Brushes.Black, 590, 250)
    2.         e.Graphics.DrawRectangle(fPen, 570, 275, 200, 30)

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I put the parathesis on the wrong one is all. Try this:
    VB Code:
    1. iX = 560 + ((200 - CInt(invSize.Width)) / 2)
    2. e.Graphics.DrawString("Invoice Number", bFont, Brushes.Black, iX, 250)

    The fact that is does the division first is what messed it up.

    Here is how I tested:
    VB Code:
    1. Dim txt As String = "Center Me Please"
    2.         Dim defFont As New Font("Tahoma", 14)
    3.         Dim boxStartx As Integer = 560
    4.         Dim boxStarty As Integer = 275
    5.         Dim boxWidth As Integer = 200
    6.  
    7.         Dim txtSize As SizeF = e.Graphics.MeasureString(txt, defFont)
    8.         Dim start As Integer = boxStartx + ((boxWidth - Convert.ToInt32(txtSize.Width)) / 2)
    9.         e.Graphics.DrawString(txt, defFont, Brushes.Black, start, boxStarty)
    10.         e.Graphics.DrawRectangle(New Pen(Color.Black), boxStartx, boxStarty, 200, 30)

  7. #7

    Thread Starter
    Hyperactive Member wolfrose's Avatar
    Join Date
    Aug 2002
    Location
    Indiana
    Posts
    309
    That's better. I changed the 560 to 570, and it is better centered. Thanks for your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width