Fixed #19617 -- Refactored form metaclasses to support more inheritance scenarios.

Thanks apollo13 and mjtamlyn for the reviews.