Solved

Right Align a DrawString

Posted on 2004-09-20
2
3,468 Views
Last Modified: 2007-11-27
Hi Guys,

I'm probably asking about the impossible but I'll give it a go anyway.

Can I right align a DrawString...?  I realise that it is drawn with x and y axes and is measured from the top-left hand corner of the DrawString.  The only way would probably be to make the DrawString measure from the top-righ hand corner and go right to left instead of left to right.

My code is below:          

text = "Net Sales Value"
e.Graphics.DrawString(text, printFont2, System.Drawing.Brushes.Black, 510, 230)

text = "Tax Value"
e.Graphics.DrawString(text, printFont2, System.Drawing.Brushes.Black, 610, 230)

Any suggestions?

Thanks in advance...

JK
0
Comment
Question by:jakekula
2 Comments
 
LVL 12

Accepted Solution

by:
fulscher earned 500 total points
ID: 12099886
JK,

Use the Graphics.MeasureString to calculate the width of the string, then offset the start position of DrawString.

Something like this:

Dim sf as SizeF

text = "Net Sales Value"
sf = MeasureString(text, printFont2)
e.Graphics.DrawString(text, printFont2, System.Drawing.Brushes.Black, 800-sf.Width, 230)

text = "Tax Value"
sf = MeasureString(text, printFont2)
e.Graphics.DrawString(text, printFont2, System.Drawing.Brushes.Black, 800-sf.Width, 230)

Hope this helps
Jan
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12100486
Hi Jakekula

it is much easier than that, just use the stringformat

        Dim recf As New RectangleF(0, 0, 300, 20)
        Dim hh As New StringFormat
        hh.Alignment = StringAlignment.Far
        g.DrawString("hello", Me.Font, New SolidBrush(Color.Black), recf, hh)

this will right align in the rectangle recf
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now