Tuesday, December 9, 2008

Using CSS - Complex Forms Without Tables? Anyone? Anyone?

I have spent the last couple weeks doing research on CSS form development for very complex forms that involve things like multiple sections and multiple columns. I often hear that "tables are evil" or that "tables should only use them for tabular data", and I was determined to find a way to incorporate CSS for my next form.

I decided to do some in depth research to determine if there was a standard or best practice for complex form development. Fortunately for me, there are thousands of examples on the web and others that can be found in various books. I decided to purchase The CSS Anthology - 101 Essential Tips, Tricks & Hacks by Rachel Andrews which has a number of great examples. I also downloaded a free book by the same publisher titled The Art & Science of CSS I also went to Borders bookstore and spent some more time perusing through about eight different CSS books. Another book by Rachel Andrew even had the promising title HTML Utopia: Designing Without Tables.

Unfortunately, I could not find a single example that would meet my requirements. Pretty much every example I encountered had a very simple form, usually a very standard one with name, address, and zip code. I could not find a single form that was more complex that involved things like multiple sections and multiple columns.

Ultimately, I have come to the conclusion that while there may be a solution for a complex form using CSS, it hasn't been documented or standardized. Until then, I plan to continue using tables to develop complex forms. If anyone out there in the blogosphere has any suggestions I am posting a screenshot of the form.


Friday, December 5, 2008

Quick and Dirty Object Sorting by Implementing IComparer<T>

Let's say you have a simple Person class with some properties of First Name and Last Name. You then populate them into a strongly typed List<T> and would like to sort them by Last Name. Fortunately there is a very easy solution.

1. Create a Person class:

public class Person
    public string FirstName {get; set;}
    public string LastName {get; set;}
    public Person

2. Create a PersonComparer class that implements IComparer<Person>:

public class PersonComparer : IComparer<Person>
    public int Compare(Person p1, Person p2)
        return String.Compare(p1.LastName, p2.LastName);

3. It was that easy. Now write some test code:

static void Main(string[] args)
    List<Person> personList = new List<Person>();
        personList.Add(new Person { FirstName = "George", LastName = "Washington"});
        personList.Add(new Person { FirstName = "Abraham", LastName = "Lincoln" });
        personList.Add(new Person { FirstName = "John", LastName = "Adams" });
        personList.Sort(new PersonComparer()); //Or use alternative below
        foreach (Person p in personList)
            Console.WriteLine("{0}, {1}", p.LastName, p.FirstName);

Alternative: If you need a one off, create a delegate that will sort the object on the fly. You will not need to implement the PersonComparer class, however it is not as reusable.

personList.Sort(delegate(Person p1, Person p2)
    return p1.LastName.CompareTo(p2.LastName);