Today, I found a reason to implement a mini CSS style builder. It’s easy to use, and pretty simple to extend. Using this private method, you can pass a Hashtable of CSS attributes and perform some type conversions. This is nice because you can benefit from design-time Intellisense by assigning, for example, a Color object to the background-color instead of a hex value.

private static ArrayList ToCssArray(Hashtable style){   ArrayList a = new ArrayList();   foreach (string k in style.Keys)   {      string s;      switch(k)      {         case "background-color":         case "color":            s = ColorTranslator.ToHtml((Color)style[k]);            break;         default:            s = (string)style[k];            break;      }      if (k!="comment")      {         a.Add(String.Format("{0}: {1}", k, s));      }      else      {                  a.Add(String.Format("/* {0} */", s));      }   }   return a;}

 Leave a Reply

(required)

(required)

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=""> <strike> <strong>

   
© 2011 Ben Allfree :: Painless Programming