Dynamic Instantiation of Forms Controls

C

Chris Chiaro

Hello everyone.
I'm trying to instatiate controls on a windows form in .net (using C#,
incidentally--not that it matters). I don't want to have to hard-code
instantiation of a certain kind of control. E.g. I _don't_ want to do this:

if ( x = 1 )
{
TextBox newControl = new TextBox();
return newControl;
}

What I'd rather do is stash the names of the control types in a database
table and instantiate the control based on the text. E.g.:

string controlName = "System.Windows.Forms.TextBox";
Assembly assemRef = Assembly.LoadFrom("mscorlib.dll");
Type controlType = assemRef.GetType("System.Windows.Forms.TextBox", true,
true);
Control newControl = (Control) Activator.CreateInstance( controlType );

I keep getting errors, however, such as (with the above code) "File or
assembly name mscorlib.dll, or one of its dependencies, was not found.",
generated from the "Assembly assemRef = " line.

I know there has to be a way to do this; I've done something similar before,
but I was instantiating a custom class, so I could reference the custom
class's compiled dll in the "Assembly assemRef = " line.

Does anyone have any ideas as to what I'm doing wrong?

Thanks,

Chris Chiaro
 
O

Olivier G. Gaumond

Chris Chiaro said:
Hello everyone.
I'm trying to instatiate controls on a windows form in .net (using C#,
incidentally--not that it matters). I don't want to have to hard-code
instantiation of a certain kind of control. E.g. I _don't_ want to do this:

if ( x = 1 )
{
TextBox newControl = new TextBox();
return newControl;
}

What I'd rather do is stash the names of the control types in a database
table and instantiate the control based on the text. E.g.:

string controlName = "System.Windows.Forms.TextBox";
Assembly assemRef = Assembly.LoadFrom("mscorlib.dll");
Type controlType = assemRef.GetType("System.Windows.Forms.TextBox", true,
true);
Control newControl = (Control) Activator.CreateInstance( controlType );

I keep getting errors, however, such as (with the above code) "File or
assembly name mscorlib.dll, or one of its dependencies, was not found.",
generated from the "Assembly assemRef = " line.

I know there has to be a way to do this; I've done something similar before,
but I was instantiating a custom class, so I could reference the custom
class's compiled dll in the "Assembly assemRef = " line.

Does anyone have any ideas as to what I'm doing wrong?

Thanks,

Chris Chiaro


Not sure if it is really what causes the problem, but the class
System.Windows.Forms.TextBox is in the assembly System.Windows.Forms.dll
not in mscorlib.dll

Olivier
 
T

Tom

Chris,

Here is some code to create controls with controlType =
System.Windows.Forms.Label, TextBox, ComboBox.

Assembly assem = Assembly.GetAssembly(typeof(Form));
Type t = assem.GetType(controlType);
object obj = Activator.CreateInstance(t);
Control tb = (Control)obj;
this.Controls.Add(tb);

Good Luck
 
C

Chris Chiaro

Olivier,

Thanks for responding.
Not sure if it is really what causes the problem, but the class
System.Windows.Forms.TextBox is in the assembly System.Windows.Forms.dll
not in mscorlib.dll
Actually, I tried referencing System.Windows.Forms.dll, too, but it didn't
work. I tried mscorlib.dll as a last, desperate attempt.

- Chris
 
C

Chris Chiaro

Tom,

That worked extremely well! I couldn't figure out how to reference the
correct assembly, but your code worked perfectly.

Thank you very much,

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top