Dynamic Instantiation of Forms Controls

  • Thread starter Thread starter Chris Chiaro
  • Start date Start date
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
 
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
 
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
 
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
 
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
 
Back
Top