Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

Transform System.Drawing.ContentAlignment property to System.Drawing. StringFormat Alignment property

Craig Vallee
Recently we had the need to transform System.Drawing.ContentAlignment property to System.Drawing. Stringformat alignment property for creating a Graphic object with text drawn on it inside a given rectangle.  Assuming that you want the text printed from left to right, you can use the ContentAlignment Enum values to obtain the desired StringAlignment Enum value.  For example:
   1: public StringFormat TransformProperty(ContentAlignment alignment)

   2:         {

   3:             StringFormat myStringFromat  = new StringFormat();

   4:             switch(alignment)

   5:             {

   6:                 case ContentAlignment.MiddleLeft:

   7:                     myStringFromat.Alignment = StringAlignment.Near;

   8:                     break;

   9:                 case ContentAlignment.MiddleRight:

  10:                     myStringFromat.Alignment = StringAlignment.Far;

  11:                     break;

  12:                 default:

  13:                     myStringFromat.Alignment = StringAlignment.Center;

  14:                     break;

  15:             }

  16:             return myStringFromat;

  17:         }

Craig

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>