Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

Setting a label's text alignment in either design mode or programmably is pretty simple. But how do you get the label's text alignment properity once its set for printing??

 

Thanks,

Dan

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

  • *Experts*
Posted

Thanks VolteFace, that'll do it. With the text alignment properity known, how can this be applied to printing? The DrawString method does not include text alignment, only text as a string.

 

Thanks,

Dan

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

  • *Experts*
Posted

Some of the overloads of DrawString accept an argument called StringFormat, this is where you can use it:

Dim strfmt As New System.Drawing.StringFormat
strfmt.Alignment = StringAlignment.Center 'you have 3 choices here

Then just pass in this as the StringFormat argument.

  • *Experts*
Posted

Also, use the LineAlignment property of StringFormat to set the vertical alignment.

 

Note that this will only work properly if you supply the DrawString method with a full rectangle to draw with, rather than just the X and Y coordinates.

  • *Experts*
Posted (edited)

Thanks mutant and VolteFace!!

 

Here's my application...

 

   Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

       'setup the bmp image
       Dim bmp As Bitmap = New Bitmap(System.Drawing.Image.FromFile _
       (Application.StartupPath & "\Data\" & DwgNumber & "a.jpg"), pnlDrawings.Width, pnlDrawings.Height)

       Dim g As Graphics = Graphics.FromImage(bmp)
       Dim lbl As Label
       Dim strfmt As New System.Drawing.StringFormat()

       For Each lbl In Me.pnlDrawings.Controls
           If lbl.Text <> Nothing Then
               If lbl.TextAlign = 1 Then
                   strfmt.Alignment = StringAlignment.Center
               ElseIf lbl.TextAlign = 2 Then
                   strfmt.Alignment = StringAlignment.Near
               ElseIf lbl.TextAlign = 3 Then
                   strfmt.Alignment = StringAlignment.Far
               End If
               g.DrawString(lbl.Text, lbl.Font, New SolidBrush(Color.Black), lbl.Location.X, lbl.Location.Y, strfmt)
           End If
       Next

etc...

 

I'm still having a problem with the alignment???

 

Thanks

Dan

Edited by DiverDan

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

  • *Experts*
Posted

Did you not see what I said in my last post? You must provide DrawString with a full rectangle (4 coordinates) to draw in. Look for the DrawString method in the MSDN for information about the different overloads.

 

After all, how can it right align text when it does know where "right" is?

  • *Experts*
Posted

Your right VolteFace...I overlooked it.

 

This appears to be working correctly now:

 

   Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

       'setup the bmp image
       Dim bmp As Bitmap = New Bitmap(System.Drawing.Image.FromFile _
       (Application.StartupPath & "\Data\" & DwgNumber & "a.jpg"), pnlDrawings.Width, pnlDrawings.Height)
       Dim g As Graphics = Graphics.FromImage(bmp)
       Dim lbl As Label
       Dim strfmt As New System.Drawing.StringFormat()

       For Each lbl In Me.pnlDrawings.Controls
           If lbl.Text <> Nothing Then
               If lbl.TextAlign = ContentAlignment.TopCenter Then
                   strfmt.Alignment = StringAlignment.Center
               ElseIf lbl.TextAlign = ContentAlignment.TopLeft Then
                   strfmt.Alignment = StringAlignment.Near
               ElseIf lbl.TextAlign = ContentAlignment.TopRight Then
                   strfmt.Alignment = StringAlignment.Far
               End If
               Dim lblRect As New RectangleF(lbl.Location.X, lbl.Location.Y, lbl.Width, lbl.Height)
               g.DrawString(lbl.Text, lbl.Font, New SolidBrush(Color.Black), lblRect, strfmt)
           End If
       Next
etc...

 

Thanks again VolteFace

Dan

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...