Solved

Trouble Creating forms dynamically

Posted on 2003-12-09
6
241 Views
Last Modified: 2010-04-16
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?
0
Comment
Question by:nlwelch
  • 4
  • 2
6 Comments
 
LVL 18

Expert Comment

by:testn
ID: 9908582
(System.Windows.Forms.TabAlignment) enum.Parse(typeof(System.Windows.Forms.TabAlignment), "Top")
0
 
LVL 18

Expert Comment

by:testn
ID: 9908595
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
 
0
 

Author Comment

by:nlwelch
ID: 9908596
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!!!

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:nlwelch
ID: 9908635
i can't get it to compile - it fails on the enum.Parse.  What am I missing?
0
 
LVL 18

Expert Comment

by:testn
ID: 9909696
oops... sorry

it should be Enum not enum.
0
 
LVL 18

Accepted Solution

by:
testn earned 500 total points
ID: 9909772
And it should be

Type t = typeof(System.Windows.Forms.Control).Assembly.GetType("System.Windows.Forms.TabAlignment");
object x = Enum.Parse(t,"Top");

object x will contain System.Windows.Forms.TabAlignment.Top
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question