Marcel
Marcel

Reputation: 101

Access property on dynamic form

How would one access properties on a dynamic form:

Type type = Type.GetType("GMD.Chart_Forms." + chart);
Form form = (Form)Activator.CreateInstance(type); 
form.PROPERTY????? = ...;
form.Show();

I know it's logical that I cannot access it directly becease the information (name of form) will only be available at runtime. But is there a way to access the properties on the dynamic form?

Upvotes: 0

Views: 65

Answers (2)

liquidsnake786
liquidsnake786

Reputation: 447

Its messy but you can check the child controls of the form until you 'reach' the control you want, CAST it to the type you are aware that it is and then access the property.

Upvotes: 0

Oliver
Oliver

Reputation: 9002

You could use dynamic:

dynamic form = Activator.CreateInstance(type);
form.Property = value;

But you might get a runtime exception if the property doesn't exist.

To avoid this you could add a check to see if the property exists:

How to detect if a property exists on an ExpandoObject?

Upvotes: 2

Related Questions