Codelog

foreach(Snippet aSnippet in CodeLog){ aSnippet.GetSolution(); }

DoUNo: When to use StringFormatFlags.DirectionRightToLeft and StringAlignment.Far

without comments

Recently I had a trouble displaying some values in a custom painted ListViewControl. I had to display some numbers right aligned. But I mistook this alignment with direction of the string, leading me to write the code as,

theArgs.Graphics.DrawString("100",
FONT_ISOVALUE_VALUE, Brushes.Black,
new Point(theArgs.Bounds.X + LOCATION_X_OFFSET, theArgs.Bounds.Y + LOCATION_Y_OFFSET),
new StringFormat(StringFormatFlags.DirectionRightToLeft));

This was working properly for positive values, but for negative values, the - sign comes after the number. i.e., instead of -100, it was displayed as 100-, thats when I understood the difference between RightToLeft text and right aligned texts. So replacing the above snippet with the following snippet does the work.

StringFormat aStringFormat = new StringFormat();
aStringFormat.Alignment = StringAlignment.Far;
theArgs.Graphics.DrawString("-100",
FONT_ISOVALUE_VALUE, Brushes.Black,
new Point(theArgs.Bounds.X + LOCATION_X_OFFSET, theArgs.Bounds.Y + LOCATION_Y_OFFSET),
aStringFormat );

So alignment is different and direction of text is different. Use StringFormat.Alignment for alignment and StringFormatFlags for direction

Written by sudarsanyes

July 7th, 2009 at 9:05 am

Posted in C#, DoUNo, UI

Tagged with , , ,

Leave a Reply