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