gezzuzz
gezzuzz

Reputation: 178

VB.net Graphics.DrawString with DirectionRightToLeft mixes up my string

here is a sample code that isolates my problem.im trying to draw a string from right to left.. if my string starts with numbers then has a comma with a letter it rearranges my string.. but if I write it without the format its fine.. open a new project and add this form to see for yourself.. thank you

Public Class Form1
    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        Dim myFont As Font = New Font("Zipper", 24, FontStyle.Bold)
        Dim myBrush As Brush = Brushes.Black
        Dim line1 As String = "220516,5130.02,N,00046.34,W,213.8,T,218.0,M,0004.6,N"
        Dim format As StringFormat = New StringFormat(StringFormatFlags.DirectionRightToLeft)
        Me.Width = 1400
        e.Graphics.DrawString(line1, myFont, myBrush, 1300, 0, format)
        e.Graphics.DrawString(line1, myFont, myBrush, 100, 50)
    End Sub
End Class

Upvotes: 2

Views: 9180

Answers (1)

LarsTech
LarsTech

Reputation: 81620

Try using the Alignment property:

Dim format As New StringFormat
format.Alignment = StringAlignment.Far

You are also not disposing your font object. For that, a simple Using bracket works well:

Using myFont As Font = New Font("Zipper", 24, FontStyle.Bold)
  Dim myBrush As Brush = Brushes.Black
  Dim line1 As String = "220516,5130.02,N,00046.34,W,213.8,T,218.0,M,0004.6,N"
  Dim format As New StringFormat
  format.Alignment = StringAlignment.Far
  Me.Width = 1400
  e.Graphics.DrawString(line1, myFont, myBrush, 1300, 0, format)
  e.Graphics.DrawString(line1, myFont, myBrush, 100, 50)
End Using

Upvotes: 3

Related Questions