Solved

Right Align a DrawString

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

831 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