Link to home
Start Free TrialLog in
Avatar of nlwelch
nlwelchFlag for United States of America

asked on

Trouble Creating forms dynamically

I am trying to build a form dynamically.  I have a class that I've created to do the dirty work.  The class accepts a form and attempts to recreate it dynamically.  The class walks through the controls and attempts to recreate them using the System.Reflection namespace.  Everything works great accept for the properties with complex types.  For example, the Alignment property is System.Windows.Forms.TabAlignment enumeration type.  How do I convert the value "Top" to the System.Windows.Forms.TabAlignment.Top enumerated value?
Avatar of testn
testn

(System.Windows.Forms.TabAlignment) enum.Parse(typeof(System.Windows.Forms.TabAlignment), "Top")
But if you use reflection, you might need to get the type first right? Then it might be

Type t = Type.GetType("System.Windows.Forms.TabAlignment");
object o =  enum.Parse(t, "Top")  

then you can use reflection setvalue to set value to System.Windows.Forms.TabAlignment.Top
 
Avatar of nlwelch

ASKER

ok - now how do I execute the same statement but with following comments:

(System.Windows.Forms.TabAlignment) - this value is retrieved from a configuration file and stored in a string.  How do I create the same typecast using a string?

typeof(System.Windows.Forms.TabAlignment) - again, this value is stored in a string.  How do I use typeof with a string variable?

Thanks for your help so far!!!

Avatar of nlwelch

ASKER

i can't get it to compile - it fails on the enum.Parse.  What am I missing?
oops... sorry

it should be Enum not enum.
ASKER CERTIFIED SOLUTION
Avatar of testn
testn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial