Solved

Right Align a DrawString

Posted on 2004-09-20
2
3,609 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

790 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