?
Solved

Trouble Creating forms dynamically

Posted on 2003-12-09
6
Medium Priority
?
246 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

850 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